Android集成极光推送Flutter

该文章只包含Andriod的集成方式

一、Flutter文档集成步骤文档地址

极光推送客户端集成插件

二、关于集成方法总结

①、创建应用

在极光推送官方网站上创建应用获取Appkey

②、安装jpush_flutter插件

在你项目的pubspec.yaml文件中的dependencies节点下添加以下代码

dependencies:
  jpush_flutter: 2.1.4

执行flutter pub get

③、配置android文件夹下的build.gradle
 defaultConfig {
        applicationId "com.example.frametrim"
        minSdkVersion 19
        targetSdkVersion 30
//        targetSdkVersion flutter.targetSdkVersion
        versionCode 1
        versionName "1.0"
        // 这里添加
        multiDexEnabled true

//极光推送开始
        ndk {
            //选择要添加的对应 cpu 类型的 .so 库。
            abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64', 'arm64-v8a'
        }

        manifestPlaceholders = [
                applicationName:"io.flutter.app.FlutterApplication",
                JPUSH_PKGNAME: applicationId,
                JPUSH_APPKEY : "你的Appkey", // NOTE: JPush 上注册的包名对应的 Appkey.
                JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.
        ]
        //极光推送结束
    }
④、接入基础推送功能
  final JPush jpush = new JPush();
  String? debugLable = 'Unknown';



  void initState() {
    super.initState();
    initPlatformState();
  }


 Future<void> initPlatformState() async {
    String? platformVersion;

    try {
      jpush.addEventHandler(
          onReceiveNotification: (Map<String, dynamic> message) async {
            print("flutter onReceiveNotification: $message");
            setState(() {
              debugLable = "flutter onReceiveNotification: $message";
            });
          }, onOpenNotification: (Map<String, dynamic> message) async {
        print("flutter onOpenNotification: $message");
        setState(() {
          debugLable = "flutter onOpenNotification: $message";
        });
      }, onReceiveMessage: (Map<String, dynamic> message) async {
        print("flutter onReceiveMessage: $message");
        setState(() {
          debugLable = "flutter onReceiveMessage: $message";
        });
      }, onReceiveNotificationAuthorization:
          (Map<String, dynamic> message) async {
        print("flutter onReceiveNotificationAuthorization: $message");
        setState(() {
          debugLable = "flutter onReceiveNotificationAuthorization: $message";
        });
      });
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    jpush.setup(
      appKey: "你自己的AppKey", //你自己应用的 AppKey
      channel: "theChannel",
      production: false,
      debug: true,
    );
    jpush.applyPushAuthority(
        new NotificationSettingsIOS(sound: true, alert: true, badge: true));

    // Platform messages may fail, so we use a try/catch PlatformException.
    jpush.getRegistrationID().then((rid) {
      print("flutter get registration id : $rid");
      setState(() {
        debugLable = "flutter getRegistrationID: $rid";
      });
    });

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      debugLable = platformVersion;
    });
  }


  • 中间遇到问题
    刚开始编译时候一直报错:
E:\Flutter\frametrim\android\app\src\debug\AndroidManifest.xml:27:9-33:20 Error:
	android:exported needs to be explicitly specified for element <receiver#com.jiguang.jpush.JPushEventReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.
E:\Flutter\frametrim\android\app\src\debug\AndroidManifest.xml:120:9-138:20 Error:
	android:exported needs to be explicitly specified for element <receiver#cn.jpush.android.service.PushReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

后来发现导致这个问题的原因是我的targetSdkVersion 设置为31 降低到30就可以了,但是问题是我同事的设置31就没问题成功编译,我的studio就是不行,具体的原因暂时没找到。

详细的设置别名之类的功能等在开发中再做整理
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是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集成极光的步骤,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值