flutter 极光推送接收消息
时间: 2023-07-31 20:04:06 浏览: 181
要在 Flutter 中接收极光推送消息,你需要做以下几个步骤:
1.在 `pubspec.yaml` 文件中添加 `jpush_flutter` 的依赖:
```yaml
dependencies:
jpush_flutter: ^1.8.6
```
2.在 `AndroidManifest.xml` 文件中添加必要的权限和配置:
```xml
<!-- 添加以下权限 -->
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- 添加以下配置 -->
<application>
<meta-data android:name="JPUSH_CHANNEL" android:value="developer-default" />
<meta-data android:name="JPUSH_APPKEY" android:value="你的 AppKey" />
<receiver android:name="cn.jpush.android.service.PushReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" />
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
<action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<service android:name="cn.jpush.android.service.DownloadService" android:enabled="true" android:exported="false" />
<service android:name="cn.jpush.android.service.PushService" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTER" />
<action android:name="cn.jpush.android.intent.REPORT" />
<action android:name="cn.jpush.android.intent.PushService" />
<action android:name="cn.jpush.android.intent.PUSH_TIME" />
</intent-filter>
</service>
<activity android:name=".MainActivity" android:launchMode="singleTop" android:configChanges="orientation|keyboardHidden|navigation" android:windowSoftInputMode="adjustResize" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="你的 scheme" android:host="你的 host" />
</intent-filter>
</activity>
</application>
```
3.在 `AppDelegate` 中设置 iOS 的推送配置:
```dart
import 'package:jpush_flutter/jpush_flutter.dart';
class AppDelegate extends NSObject with FlutterAppDelegate {
final JPush jpush = JPush();
@override
bool application(
UIApplication application,
didFinishLaunchingWithOptions launchOptions
) {
jpush.setup(
appKey: "你的 AppKey",
channel: "developer-default",
production: false,
debug: true,
);
jpush.applyPushAuthority(
notificationSettingsIOS: const NotificationSettingsIOS(
alert: true,
badge: true,
sound: true,
),
);
return super.application(application, didFinishLaunchingWithOptions: launchOptions);
}
}
```
4.在需要接收推送消息的页面中监听消息:
```dart
import 'package:jpush_flutter/jpush_flutter.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final JPush jpush = JPush();
@override
void initState() {
super.initState();
jpush.addEventHandler(
onReceiveNotification: (Map<String, dynamic> message) async {
print("接收到推送消息:$message");
},
);
}
@override
void dispose() {
jpush.removeEventHandler();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('极光推送 Demo'),
),
body: Center(
child: const Text('这是一个极光推送 Demo'),
),
);
}
}
```
以上就是在 Flutter 中接收极光推送消息的基本步骤,具体实现还需要根据自己的需求进行调整。
阅读全文
相关推荐













