首页 > 文章 > Flutter接入极光推送

Flutter接入极光推送

(1)搜索 https://pub.dartlang.org/packages/jpush_flutter ,安装插件,并且按照官方配置 /android/app/build.gradle

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

    manifestPlaceholders = [
        JPUSH_PKGNAME : applicationId,
        JPUSH_APPKEY : "appkey", // NOTE: JPush 上注册的包名对应的 Appkey.
        JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
    ]
  }    
}

(2)Flutter 新建页面,我放在Welcome页面

class SplashPageState extends State<SplashPage> implements OnSkipClickListener {
  JPush jPush = new JPush();
  String registerId;
  _startupJpush() {
    jPush.setup(appKey: "c52495cbcbc37ee42f04e751", channel: "developer-default",debug: true);
  }

  _getRegisterID() async {
    registerId = await jPush.getRegistrationID();
    print(‘*********registerid=‘ + registerId);
    return registerId;
  }

  _setPushTag() {
    List<String> tags = List<String>();
    tags.add("jason");
    jPush.setTags(tags);
  }

  _addEventHandler() {
    // Future<dynamic>event;
    jPush.addEventHandler(onReceiveNotification: (Map<String, dynamic> event) {
      print(‘*********addOnreceive>>>>>>$event‘);//进程运行时候可以接受
      var title = event[‘alert‘];
      var extra = json.decode(event[‘extras‘][‘cn.jpush.android.EXTRA‘]);
      notifyRoute(extra[‘type‘],title,extra[‘id‘]);
      print(‘*********msg:$event‘);
    }, onOpenNotification: (Map<String, dynamic> event) {
      print(‘*********addOpenNoti>>>>>$event‘); //进程关闭的时候可以接受
      var title = event[‘alert‘];
      var extra = json.decode(event[‘extras‘][‘cn.jpush.android.EXTRA‘]);
      notifyRoute(extra[‘type‘],title,extra[‘id‘]);
    }, onReceiveMessage: (Map<String, dynamic> event) {
      print(‘*********addReceiveMsg>>>>>$event‘); //进程运行时候可以接受
      print(event.toString());
      var jsStr = json.decode(event.toString());
    });
}

//推送跳转
void notifyRoute(String type,String title,String id) {
  if(type!=null) {
    switch(type){
    case ‘news‘:
      Navigator.of(context).push(new MaterialPageRoute(builder: (ctx) => new WebView(title: title, url:id )));
      break;
    case ‘tmdetail‘:
      Navigator.of(context).push(new MaterialPageRoute(builder: (ctx) => new DetailPage(tmId: int.parse(id))));
      break;
  }
  }
}

@override
void initState() {
// TODO: implement initState
  super.initState();
  _startupJpush();
  _setPushTag();
  _addEventHandler();
  _getRegisterID();
  _getWelcomeImage();
}
}

(3) 我的用是Extras 并且在客户端 通过参数 进行指定页面跳转

(4)服务端推送,C# Core版本 nuget搜索JiGuang.JPush

/// <summary>
/// 极光推送全局推送
/// </summary>
/// <param name="title">标题</param>
/// <param name="content">正文</param>
/// <param name="dic">extras字典</param>
/// <param name="indent">指定安卓页面,没用</param>
/// <param name="outmsg"></param>
/// <returns></returns>
public static bool Send(string title,string content, Dictionary<string,object> dic, Dictionary<string, object> indent,out string outmsg)
{
  var client = new Jiguang.JPush.JPushClient(appKey,appSecret);
  var android = new Android { Title = title, Alert = content, AlertType = 0, Extras = dic, Indent = indent };
  var ios = new IOS{ Alert= title, Extras= dic };
  var pushInfo = HD.DLL.Push.GetTopInfo();
  if(pushInfo!=null&&pushInfo.Id>0 )
  {
  var ts = System.DateTime.Now.Subtract(pushInfo.Addtime);
  if(ts.Hours<1)
  {
    outmsg = "最近的全局推送时间:"+pushInfo.Addtime+",请间隔一个小时";
    return false;
  }
  }
  var playLoad = new Jiguang.JPush.Model.PushPayload() {
    Platform = "all",
    //Audience = "all",
    Notification = new Notification() { Alert=title, Android =android,IOS=ios},
    Message = new Jiguang.JPush.Model.Message() { Content=content,Title=title,Extras= dic }
  };
  var response=client.SendPush(playLoad);
  outmsg = response.Content;
  var ret= response.StatusCode== System.Net.HttpStatusCode.OK;

  //记录推送日志
  pushInfo.Addtime = System.DateTime.Now;
  pushInfo.Title = title;
  pushInfo.Content = content;
  pushInfo.UserId = 0;
  pushInfo.Extras = JsonConvert.SerializeObject(dic);
  pushInfo.Status = ret ? 1 : -1;
  Push.Add(pushInfo);
  return ret;
}

 

 
时间:2019-01-25 10:43:45阅读(2057)
联系我们 - 留言反馈
© 2017 版权所有 鲁ICP备17052893号