cordova做消息推送jpush-phonegap-plugin

官方文档:https://github.com/jpush/jpush-phonegap-plugin

简单的写法直接上代码

function onDeviceReady() {
    alert('ready');
    document.addEventListener("jpush.receiveRegistrationId", function (event) {
        alert("receiveRegistrationId" + JSON.stringify(event));
    }, false)
    initiateUI();
};

function getRegistrationID() {
    window.JPush.getRegistrationID(onGetRegistrationID);
}
function onGetRegistrationID(data) {
    alert("JPushPlugin:registrationID is " + data);
    if (data.length == 0) {
        var t1 = window.setTimeout(getRegistrationID, 1000);
    }
};

function onTagsWithAlias(event) {
    console.log("onTagsWithAlias");
    var result = "result code:" + event.resultCode + " ";
    result += "tags:" + event.tags + " ";
    result += "alias:" + event.alias + " ";
};

function onOpenNotification(event) {
    alert('onOpenNotification');
    var alertContent;
    if (device.platform == "Android") {
        alertContent = event.alert;
    } else {
        alertContent = event.aps.alert;
    }
};

function onReceiveNotification(event) {
    alert('ReceiveNotification');
    var alertContent;
    if (device.platform == "Android") {
        alertContent = event.alert;
    } else {
        alertContent = event.aps.alert;
    }
};

function onReceiveMessage(event) {
    alert('onReceiveMessage');
    var message;
    if (device.platform == "Android") {
        message = event.message;
    } else {
        message = event.content;
    }
};

function initiateUI() {
    alert('initiateUI')
    alert('window.JPush= '+ window.JPush)
    alert('window.JPush= '+ window.plugins)
    if(!!window.JPush){
        window.JPush.init();
        alert('init完成1')
    }else{
        window.plugins.jPushPlugin.init();
        alert('init完成2');
    }
    window.JPush.resumePush();//恢复推送
    window.JPush.setDebugMode(true);
    window.setTimeout(getRegistrationID, 1000);
    if (device.platform != "Android") {
        window.JPush.setApplicationIconBadgeNumber(0);
    }
};


document.addEventListener("deviceready", onDeviceReady, false);
document.addEventListener("jpush.openNotification", onOpenNotification, false);
document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);
document.addEventListener("jpush.receiveMessage", onReceiveMessage, false);

设置别名和标签

window.JPush.setAlias({ sequence: 1, alias: 'your_alias' });
window.JPush.setTags({ sequence: 1, tags: ['tag1', 'tag2'] });
遇到的问题
1. registrationID获取不到

这是个神奇的问题,我跟同事试了很多次,最后的解决办法是重装android和jpush-phonegap-plugin插件,并且还要remove干净,如下~~

cordova plugin remove jpush-phonegap-plugin --variable APP_KEY=bd902aea...
2. 消息图标显示的是cordova的logo

需要改D:\demo\applu\platforms\android\app\src\main\res这个路径下的图标

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值