flutter 集成极光推送

时间: 2023-07-29 13:06:20 浏览: 73
好的,下面是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集成极光推送的步骤,希望能对你有所帮助。

相关推荐

最新推荐

recommend-type

Flutter应用集成极光推送的实现示例

主要介绍了Flutter应用集成极光推送的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

flutter RotationTransition实现旋转动画

主要为大家详细介绍了flutter RotationTransition实现旋转动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Flutter 完美的验证码输入框实现

主要介绍了Flutter 完美的验证码输入框实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

Flutter ApprenticeFlutter Apprentice - 2nd Version最新版本

Flutter Apprentice Flutter Apprentice - 2nd Version最新版本 Flutter Apprentice By Vincenzo Guzzi, Kevin D Moore, Vincent Ngo and Michael Katz Build for both iOS and Android with Flutter! Flutter is a...
recommend-type

Flutter完整开发实战详解 PDF

Flutter完整开发实战详解 前⾔ ⼀、Dart语⾔和Flutter基础 ⼆、 快速开发实战篇 三、 打包与填坑篇 四、 Redux、主题、国际化 五、 深⼊探索 六、 深⼊Widget原理 七、 深⼊布局原理 ⼋、 实⽤技巧与填坑
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。