官方文档: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
这个路径下的图标