zoukankan      html  css  js  c++  java
  • Cordova 集成极光推送

    1、申请极光推送账号,创建应用,配置包等信息,可以获得AppKey,用于添加Cordova插件,这部分暂不细讲,根据官网的提示操作就能完成。

    2、命令窗口给cordova项目添加极光推送插件

    cordova plugin add jpush-phonegap-plugin --variable APP_KEY=xxxxxxxxxxxxxxxxxxxxx

    3、项目中使用

    由于项目原本是web改过来的,并没有使用单页的方式,所以遇到不少坑,index.js只需要在首页面加载即可,JPush.js则需要在每个页面都注册一遍极光推送事件监听,不然不会触发。

    index.js    在程序初始化的时候初始化极光推送

    console.log("===[开始初始化]===");
    var app = {
        initialize: function() {
            console.log("===[注册初始化事件]===");
            document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
        },
        onDeviceReady: function() {//设备准备完毕
            console.log("===[设备准备就绪]===");
            initJPush();//初始化极光推送
        }
    };
    app.initialize();
    
    function initJPush(){
            console.log("===[初始化极光推送]===");
            try {
                window.JPush.init();
                window.JPush.setDebugMode(true);
                if (device.platform != "Android") {
                    window.JPush.setApplicationIconBadgeNumber(0);
                }
            } catch (exception) {
                alert(exception);
            }
            document.addEventListener("jpush.receiveRegistrationId", function (event) {
                console.log("receiveRegistrationId" + JSON.stringify(event));
            }, false);
        }
       function setAlias(alias){
            console.log("======[alias]====:"+alias);
            window.JPush.setAlias({ sequence: 1, alias: alias },
            function (result) {
                console.log("alias设置成功:"+result.alias);
            }, function (error){
                alert("err:"+error.code);
            });
        }

    JPush.js

     document.addEventListener("jpush.openNotification", function(){
        //打开通知
        try {
            //获取极光推送附带的参数
            var id= event.extras.id;
            var lx= event.extras.lx;
            //相应的动作
        } catch (exception) {
            console.log("JPushPlugin:onOpenNotification" + exception);
        }
    }, false);
    document.addEventListener("jpush.receiveNotification", function(){
        //收到通知
        try {
            //获取极光推送附带的参数
            var id= event.extras.id;
            var lx= event.extras.lx;
            //相应的动作
        } catch (exception) {
            console.log("JPushPlugin:onReceiveNotification" + exception);
        }
    }, false);        
  • 相关阅读:
    解决ValueError: Some of types cannot be determined by the first 100 rows,
    关于number_format的一个报错
    关于IPV6审核被拒问题处理
    项目集成三方库由静态库转为动态库遇到的问题总结
    Swift判断对象属于什么类型
    运行项目报错 报scandir() has been disabled for security reasons
    CocoPods port 443
    Jetpack学习-WorkManager
    Jetpack学习-Paging
    Jetpack学习-Navigation
  • 原文地址:https://www.cnblogs.com/raym/p/10281126.html
Copyright © 2011-2022 走看看