极光推送
使用(以下基于ionic项目进行使用)
1.创建ionic 项目
ionic start myApp tabs
2.添加平台
首先修改应用ID 使其与推送的包名对应 (config.xml 里的 widget id)
ionic platform add android
3.添加插件(注意修改APIKey)
cordova plugin add https://github.com/jpush/jpush-phonegap-plugin.git --variable API_KEY=your_jpush_appkey
4.插件的初始化(参照service.js)
window.plugins.jPushPlugin.init();
5.注意设置手机的自启管理权限
service.js
angular.module('starter.services',[])
.service('AppService',function(){
//收到通知调用
var onReceiveNotification = function(event){
try{
var alertContent
if(device.platform == "Android"){
alertContent = window.plugins.jPushPlugin.receiveNotification.alert;
}else{
alertContent = event.aps.alert;
}
console.log('document onReceiveNotification');
console.log(alertContent);
}catch(exeption){
console.log(exception)
}
};
//打开通知调用
var onOpenNotification = function(event){
try{
var alertContent
if(device.platform == "Android"){
alertContent=window.plugins.jPushPlugin.openNotification.alert;
}else{
alertContent = event.aps.alert;
}
console.log('document receiveNotification');
console.log(alertContent);
}
catch(exception){
console.log("JPushPlugin:onOpenNotification"+exception);
}
};
//收到信息调用
var onReceiveMessage = function(event){
try{
var message
if(device.platform == "Android"){
message = window.plugins.jPushPlugin.receiveMessage.message;
}else{
message = event.content;
}
console.log('document openNotification');
console.log(message);
}
catch(exception){
console.log("JPushPlugin:onReceiveMessage-->"+exception);
}
};
var onTagsWithAlias = function(event){
try{
console.log("onTagsWithAlias");
var result="result code:"+event.resultCode+" ";
result+="tags:"+event.tags+" ";
result+="alias:"+event.alias+" ";
console.log(result)
}
catch(exception){
console.log(exception)
}
};
this.initPush = function(){
try{
window.plugins.jPushPlugin.init();
//用于开启调试模式,可以查看集成JPush过程中的log,如果集成失败,可方便定位问题所在
window.plugins.jPushPlugin.setDebugMode (true)
//用于android收到应用内消息的回调函数(请注意和通知的区别),该函数不需要主动调用
window.plugins.jPushPlugin.receiveMessageInAndroidCallback = function(data){
console.log('receiveMessageInAndroidCallback');
console.log(data);
};
//当点击android手机的通知栏进入应用程序时,会调用这个函数,这个函数不需要主动调用,是作为回调函数来用的
window.plugins.jPushPlugin.openNotificationInAndroidCallback = function(data){
console.log('openNotificationInAndroidCallback');
console.log(data);
}
document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);
document.addEventListener("jpush.setTagsWithAlias", onTagsWithAlias, false);
//若有 openNotificationInAndroidCallback 则此方法不调用
document.addEventListener("jpush.openNotification", onOpenNotification, false);
//若有 receiveMessageInAndroidCallback 则此方法不调用
document.addEventListener("jpush.receiveMessage", onReceiveMessage, false);
}catch(exception){
console.log("JPushPlugin:init-->"+exception);
}
};
//tags 为数组,alias 为字符串
this.setTagsWithAlias = function(tags,alias){
try{
window.plugins.jPushPlugin.setTagsWithAlias(tags,alias);
}catch(exception){
console.log("JPushPlugin:setTagsWithAlias-->"+exception);
}
}
});
相关网站
极光控制台
https://www.jpush.cn/push/apps/4da4d7c81227935424099c97/push/notification/sent