极光推送有flutter的吗
时间: 2024-06-06 07:05:31 浏览: 141
是的,极光推送支持Flutter。极光推送提供了Flutter插件,可以在Flutter应用中使用极光推送服务。你可以在Flutter插件市场中搜索“jpush_flutter”来获取极光推送的Flutter插件。同时,极光推送也提供了详细的文档和示例代码,以帮助你在Flutter应用中集成极光推送服务。
相关问题
flutter 集成极光推送
好的,下面是Flutter集成极光推送的步骤:
1. 在极光官网注册账号并创建应用,获取AppKey。
2. 在Flutter项目中添加极光推送插件,如:jpush_flutter。
3. 在AndroidManifest.xml文件中添加以下代码:
```xml
<!-- 极光推送权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 极光推送服务 -->
<service
android:name="cn.jpush.android.service.PushService"
android:enabled="true"
android:exported="false"
android:process=":pushcore" >
<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>
<receiver
android:name="cn.jpush.android.service.PushReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter android:priority="1000">
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
<category android:name="你的包名" />
</intent-filter>
<intent-filter>
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED_PROXY" />
<category android:name="你的包名" />
</intent-filter>
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" />
<category android:name="你的包名" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<category android:name="你的包名" />
</intent-filter>
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<category android:name="你的包名" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
```
4. 在iOS项目中,到极光官网下载JPush SDK并导入到项目中,在AppDelegate.m文件中添加以下代码:
```objective-c
#import "JPUSHService.h"
// iOS 10 及以上需导入 UserNotifications.framework
#import <UserNotifications/UserNotifications.h>
// Override point for customization after application launch.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 初始化 JPush SDK
[JPUSHService setupWithOption:launchOptions appKey:@"your_appkey" channel:nil apsForProduction:NO];
return YES;
}
// 注册APNs成功并上报DeviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[JPUSHService registerDeviceToken:deviceToken];
}
// 实现注册APNs失败接口
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
// 添加处理APNs通知回调方法
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
// iOS 10之前收到通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Required, iOS 7 Support
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
// iOS 10之前收到本地通知
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// Required, iOS 7 Support
[JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
}
// iOS 10及以上收到通知
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
}
// iOS 10及以上收到本地通知
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler();
}
#endif
```
5. 在Flutter代码中,初始化JPush并监听推送事件:
```dart
import 'package:jpush_flutter/jpush_flutter.dart';
// 初始化JPush
JPush jpush = new JPush();
// 监听推送事件
jpush.addEventHandler(
onReceiveNotification: (Map<String, dynamic> message) async {
print("Flutter onReceiveNotification: $message");
},
onOpenNotification: (Map<String, dynamic> message) async {
print("Flutter onOpenNotification: $message");
},
onReceiveMessage: (Map<String, dynamic> message) async {
print("Flutter onReceiveMessage: $message");
},
);
// 启动JPush
jpush.setup(
appKey: "your_appkey", // 极光官网申请的AppKey
channel: "developer-default", // 渠道,默认值为“developer-default”
production: false, // 是否生产环境
debug: true, // 是否开启调试模式
);
```
以上就是Flutter集成极光推送的步骤,希望能对你有所帮助。
flutter 极光推送接收消息
接收极光推送消息需要进行以下步骤:
1. 集成 Flutter 极光推送插件,可以使用官方提供的 `jpush_flutter` 插件。
2. 在 `main.dart` 文件中初始化极光推送,并设置监听回调函数。
```dart
import 'package:jpush_flutter/jpush_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
JPush jpush = new JPush();
jpush.setup(
appKey: "yourAppKey",
channel: "developer-default",
production: false,
debug: true,
);
// 设置推送监听回调函数
jpush.addEventHandler(
onReceiveNotification: (Map<String, dynamic> message) async {
print("Received notification: $message");
},
onOpenNotification: (Map<String, dynamic> message) async {
print("Opened notification: $message");
},
onReceiveMessage: (Map<String, dynamic> message) async {
print("Received message: $message");
},
);
runApp(MyApp());
}
```
3. 在回调函数中处理推送消息。`onReceiveNotification` 回调函数处理通知栏消息,`onOpenNotification` 回调函数处理用户点击通知栏消息的操作,`onReceiveMessage` 回调函数处理自定义消息。
```dart
// 处理通知栏消息
onReceiveNotification: (Map<String, dynamic> message) async {
print("Received notification: $message");
// 在这里处理消息内容,例如弹窗提示用户
},
// 处理用户点击通知栏消息
onOpenNotification: (Map<String, dynamic> message) async {
print("Opened notification: $message");
// 在这里处理消息内容,例如打开指定页面
},
// 处理自定义消息
onReceiveMessage: (Map<String, dynamic> message) async {
print("Received message: $message");
// 在这里处理消息内容,例如弹窗提示用户
},
```
注意:以上回调函数都是可选的,可以根据具体需求来决定是否需要实现。同时,如果你的应用程序在后台运行时收到推送消息,这些回调函数将不会被调用,而是会在用户点击通知栏消息时被调用。
阅读全文