ionic4 集成极光推送jpush
1. 在极光官网注册、登录、创建应用
极光推送官网
应用包名要与config.xml一致
2.安装插件
ionic cordova plugin add jpush-phonegap-plugin --variable APP_KEY=your_jpush_appkey
//your_jpush_appkey是应用信息的AppKey
npm install --save @jiguang-ionic/jpush
插件地址
3.引入app
在app.module.ts文件中引入:
import { JPush } from '@jiguang-ionic/jpush/ngx';
providers: [
...
JPush,
...
]
在app.component.ts文件中引入
import { JPush } from '@jiguang-ionic/jpush/ngx';
export class AppComponent {
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
private jpush:JPush
) {
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.jpush.setDebugMode(true);
this.jpush.init();
});
}
}
在首页ts里加入
// 点击极光推送
var that = this;
document.addEventListener("jpush.openNotification", function (event) {
var alertContent = event['extras'];
alert(alertContent)
}, false)
// 获取极光推送
document.addEventListener("jpush.receiveNotification", function (event) {
}, false)
this.getRegistrationID();
this.setAlias(this.userInfo.phone);
getRegistrationID() {
this.jPushPlugin.getRegistrationID().then(res => {
}).catch()
}
setAlias(alias: string) {
this.jPushPlugin.setAlias({ sequence: 1, alias: alias })
.then(res => {
})
.catch(err => {
});
}
用户第一次在手机上打开APP的时候,会初始化极光插件,然后极光插件会为手机设备设置一个Registration ID。必须获取到Registration ID,才能为用户打标签和别名。一般都是能够成功获取到Registration ID的。
用户登录成功后,我们要为用户打上对应tag,这样用户才能接收到推送。
4.打包apk,安装到手机测试
踩坑(一)
报错 Failed to post notification on channel "JPush_Notification"
在JPushPlugin.java文件中引入包名
踩坑(二)
APP_KEY不是合法的值
解决办法:
修改Resources/PushConfig.plist 修改对应的APP_KEY和CHANNEL(渠道)
踩坑(三)
Not get deviceToken yet. After successful login, a custom message can be sent, but the APNs notification cannot. Until the deviceToken is obtained and reported successfully, the APNs notification can be used normally.
解决办法:
把手机网络关掉重新联网就好了。
网上很多遇到这个问题的小伙伴是因为didRegisterForRemoteNotificationsWithDeviceToken这个方法被覆盖了,极光的registerDeviceToken方法没被调用,没把设备Token传过去。但有的设备不行,有的设备正常,就检查一下设备的网络吧,百分百是网络设置出问题了。
关闭推送
当用户退出当前账号、token失效或发生了互踢,则需要关闭jpush,这样用户的手机上才不会继续收到推送。
注意:推送只会推送给一台设备。在没有开启互踢的情况下,多台手机登录同一个账号,这时候是无法确定推送会发送给哪台手机的。
注意:IOS的通知只有在应用处于后台或没开启的时候才会在通知栏显示。
标签设置错误码定义
|Code|描述|详细解释| |:---:|:---:|:---:| |6001|无效的设置,tag/alias 不应参数都为|null| |6002|设置超时|建议重试 |6003|alias 字符串不合法|有效的别名、标签组成:字母(区分大小写)、数字、下划线、汉字、特殊字符(v2.1.6支持)@!#$&+=.¥ |6004|alias超长。最多 40个字节|中文 UTF-8 是 3 个字节 |6005|某一个 tag 字符串不合法|有效的别名、标签组成:字母(区分大小写)、数字、下划线、汉字、特殊字符(v2.1.6支持)@!#$&+=.¥ |6006|某一个 tag 超长。一个 tag 最多 40个字节|中文 UTF-8 是 3 个字节 |6007|tags 数量超出限制。最多 1000个|这是一台设备的限制。一个应用全局的标签数量无限制。 |6008|tag 超出总长度限制|总长度最多 7K 字节 |6009|未知错误|由于权限问题,导致的PushService启动异常。 |6011|10s内设置tag或alias大于10次|短时间内操作过于频繁 |6012|在JPush服务stop状态下设置了tag或alias|3.0.0版本新增的错误码。