flutter集成极光推送

878 阅读1分钟

注册极光账号

登记极光官网www.jiguang.cn/pushsource=…

点击应用管理 image.png

点击创建应用,填写应用信息后点击确定 image.png

保存AppKey(项目需要配置) image.png

填写项目包名(注意填写后不能修改)有华为oppo等厂商key可以配置提供推送效果 image.png

在flutter项目的 pubspec.yaml 中加入 依赖包

dependencies:  
  #极光推送 
  jpush_flutter: 2.2.2

在main初始化极光推送

///初始化极光推送
void startJPush() {
  JPush jpush = JPush();
  //配置jpush(不要省略)
  //debug就填debug:true,生产环境production:true
  jpush.setup(
      appKey: '替换极光注册的appKey',
      channel: 'developer-default',
      production: true,
      debug: true);
  //监听jpush(ios必须配置)
  jpush.applyPushAuthority(
      const NotificationSettingsIOS(sound: true, alert: true, badge: true));
  jpush.addEventHandler(
    onReceiveNotification: (Map<String, dynamic> message) async {
      print('message11:$message');
    },
    onOpenNotification: (Map<String, dynamic> message) async {
      //点击通知栏消息,在此时通常可以做一些页面跳转等
      print('message22:$message');
    },
  );
}

安卓配置:

在 /android/app/build.gradle 中添加下列代码

android: {
    ....
    defaultConfig {
        applicationId "替换成自己应用 ID"
        ...
        ndk {
            //选择要添加的对应 cpu 类型的 .so 库。
            abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64', 'arm64-v8a',
        }

        //极光推送配置key
        manifestPlaceholders = [
                JPUSH_PKGNAME : applicationId,
                JPUSH_APPKEY : "b75e994247ba8107f3f1c23f", // NOTE: JPush 上注册的包名对应的 Appkey.
                JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.

//                MEIZU_APPKEY : "MZ-魅族的APPKEY",
//                MEIZU_APPID : "MZ-魅族的APPID",
//                XIAOMI_APPID : "MI-小米的APPID",
//                XIAOMI_APPKEY : "MI-小米的APPKEY",
//                OPPO_APPKEY : "OP-oppo的APPKEY",
//                OPPO_APPID : "OP-oppo的APPID",
//                OPPO_APPSECRET : "OP-oppo的APPSECRET",
//                VIVO_APPKEY : "vivo的APPKEY",
//                VIVO_APPID : "vivo的APPID"
        ]
    }
}

如果需要配置指定厂商key还需要下面配置 model的build.gradle配置

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"


    // 接入华为厂商
//    implementation 'com.huawei.hms:push:4.0.2.300'
//    implementation 'cn.jiguang.sdk.plugin:huawei:4.0.0'// 极光厂商插件版本与接入 JPush 版本保持一致,下同
//    // 接入 FCM 厂商
//    implementation 'com.google.firebase:firebase-messaging:21.0.1'
//    implementation 'cn.jiguang.sdk.plugin:fcm:4.0.0'
//    // 接入魅族厂商
//    implementation 'cn.jiguang.sdk.plugin:meizu:4.0.0'
//    // 接入 VIVO 厂商
//    implementation 'cn.jiguang.sdk.plugin:vivo:4.0.0'
//    // 接入 OPPO 厂商
//    implementation 'cn.jiguang.sdk.plugin:oppo:4.0.0'
//    // 接入小米厂商
//    implementation 'cn.jiguang.sdk.plugin:xiaomi:4.0.0'
}

项目的build.gradle配置

repositories {
    google()
    mavenCentral()
    maven { url 'https://maven.aliyun.com/repository/public/'}
    maven { url 'https://maven.aliyun.com/repository/spring/'}
    maven { url "https://www.jitpack.io" }
    maven { url "http://download.flutter.io"}
}
allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url 'https://maven.aliyun.com/repository/public/'}
        maven { url 'https://maven.aliyun.com/repository/spring/'}
        maven { url "https://www.jitpack.io" }
        maven { url "http://download.flutter.io"}
    }
}

IOS配置:

配置极光的tag和别名还有ios端的证书配置稍后更新
ios苹果端并不需要改动代码,只需要配置证书开启服务就行 image.png

跳转指定页面可以在推送消息配置附加字段 image.png

在下面监听方法可以获取配置

jpush.addEventHandler(
  onReceiveNotification: (Map<String, dynamic> message) async {
    //推送通知栏新消息
    print('message11:$message');
  },
  onOpenNotification: (Map<String, dynamic> message) async {
    //点击通知栏消息,在此时通常可以做一些页面跳转等
    print('message22:$message');
    var text=message["extras"];
    print('text:$text');
    var text2=text["cn.jpush.android.EXTRA"];//sdk里面固定key
    print('text2:$text2');
    var text3=text["key"];//key是推送附件字段配置的key
  },
);