Flutter 直接插件集成极光推送,点击推送冷启动

之前做过一版是iOS 集友盟推送的版本,是原生的但是支持iOS版本的
iOS 集友盟推送的版本
此篇介绍直接通过yaml,插件安装更加方便

安装方式按照参照官方介绍即可

极光推送
jpush_flutter: 0.1.0

使用部分

import 'package:jpush_flutter/jpush_flutter.dart';


// Platform messages are asynchronous, so we initialize in an async method.
  Future initPlatformState() async {
    String platformVersion;

    try {
      jpush.addEventHandler(
          onReceiveNotification: (Map message) async {
        print("flutter onReceiveNotification: $message");
      }, onOpenNotification: (Map message) async {
        print("flutter onOpenNotification: $message");

        print(message);
        /*
        {aps: {alert: {title: 标题, body: 内容}, badge: 1, sound: default}, extras: {type: 2}, _j_uid: 36574310480, _j_msgid: 38280645211627628, type: 2, _j_business: 1}
        * */

//        if(message['type']!=null){
//          int type = int.parse(message['type']);
//          print(type);
//          receiveRemoteMsg(type);
//        }
        String reslut = message.toString();
        reslut = _getTypeValue(reslut);
        if (reslut.length == 1) {
          int type = int.parse(reslut);
          receiveRemoteMsg(type);
        }
      }, onReceiveMessage: (Map message) async {
        print("flutter onReceiveMessage: $message");
      });
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    jpush.setup(
      appKey: "xxxxxxxx", //你自己应用的 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");
     
      Future.delayed(Duration(seconds: 1), () {
        getNotice();
      });
    });

    //清除appicon右上角通知角标
    jpush.setBadge(0);
  }
//点击推送启动APP
/*
 /// iOS Only
    /// 点击推送启动应用的时候原生会将该 notification 缓存起来,该方法用于获取缓存 notification
    /// 注意:notification 可能是 remoteNotification 和 localNotification,两种推送字段不一样。
    /// 如果不是通过点击推送启动应用,比如点击应用 icon 直接启动应用,notification 会返回 @{}。
    /// @param {Function} callback = (Object) => {}
    ///
*/
  void getNotice() async {
    Map message = await jpush.getLaunchAppNotification();
  
    String reslut = message.toString();
    reslut = _getTypeValue(reslut);
//    Toast.show(reslut);
    if (reslut.length == 1) {
      int type = int.parse(reslut);
      receiveRemoteMsg(type);
    }
  }

  String _getTypeValue(String msg) {
    List list = msg.split("type");
    if (list.length > 1) {
      String s = list[1];
      for (var i = 0; i < s.length; i++) {
        var c = s.substring(i, i + 1);
        if (c == '2' || c == '3' || c == '5' || c == '6'|| c == '8') {
          return c;
        }
      }
    }
    return '--';
  }

  //接收到的推送
  void receiveRemoteMsg(int type) {
    print('打开推送: $type');

    if (PersonInfo.instance().islogin == 1) {
      Future.delayed(Duration(seconds: 2), () {
        switch (type) {
          case 2:
            Navigator.pushNamed(
                YJRouters.appContext, YJRouters.notice_against_page);
            break;
          case 3:
            Navigator.pushNamed(
                YJRouters.appContext, YJRouters.notice_yearcheak_page);
            break;
          case 5:
            Navigator.pushNamed(
                YJRouters.appContext, YJRouters.notice_insurance_page);
            break;
          case 6:
            Navigator.pushNamed(
                YJRouters.appContext, YJRouters.notice_upkeep_page);
            break;
          case 8:
            Navigator.pushNamed(
                YJRouters.appContext, YJRouters.order_wait_page);
            break;
        }
      });
    }
  }

点击推送冷启动App.png

其实就要就是这块的逻辑吧,点击推送冷启动,通过这样的方法就可以处理了,这里主要是针对的iOS用,Android 不存在这样的逻辑,因为Android的进程杀死了就无法收到推送了,所以应该不需要处理。

你可能感兴趣的:(Flutter 直接插件集成极光推送,点击推送冷启动)