android 极光推送解绑,app集成极光推送笔记(angular js)

出处:极光推送官方文档以及github上的文档

1.安装

一般使用cordova安装(其他安装方式详见文档),命令行输入:

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

安装之后就可以直接使用,不需要window.JPush.init(),并且以下操作一般放在app启动时进行,并且需要在cordova的ondeviceready事件中。

2.获取RegistrationID

window.JPush.getRegistrationID(function(data) {

$rootScope.registrationID=data;

$log.debug("JPushPlugin:registration ID:"+$rootScope.registrationID);

});

获取到的设备id用来进行设备绑定,可以用$rootScope上的一个变量来存储,项目中发现android设备第一次获取RegistrationID是空的第二次就好了,ios正常,不知道什么原因。

3.设置别名(alias)与标签(tags)

别名和标签也是用来进行设备绑定的,一般别名选用例如用户名,标签就例如Student或者teacher等等,可为每个用户打多个标签,所以标签是一个数组,设置了别名和标签后可以通过jpush.setTagsWithAlias事件来监听设置是否成功,其中的resultCode可以参考github文档中的错误定义。

var tags = newArray();

tags[0] = "student";

window.JPush.setTagsWithAlias(tags, username);//设置别名与标签

document.addEventListener("jpush.setTagsWithAlias", function (event) { //设置别名标签的监听事件

$log.debug("result code:" + event.resultCode + "tags:" + event.tags + "alias:" +event.alias);

},false);

20180110215742463690.png

4.设备与服务器的绑定

上面所获取到的RegistrationID、别名、标签都是用来进行设备绑定的必须字段,具体项目中需要什么就需要与后台进行沟通了,接着调用后台提供的绑定接口进行设备绑定,以便于可以收到推送消息。

5.获取点击通知内容

下一步就是收到推送消息,点击消息获取相应数据,点击事件如下:

document.addEventListener("jpush.openNotification", function (event) {}, false);

不同平台有不同的获取数据方式,例如:

Android

$scope.jpushToDetailData={"messageId":event.extras.push_page_url,"openMessageType":event.extras.push_type

};

IOS

$scope.jpushToDetailData={"messageId":event.push_page_url,"openMessageType":event.push_type

};

可以看出Android在获取数据时多了一层extras。要注意的是这个事件也是放在cordova的ondeviceready事件中,并且一定要放在整个项目最开始的位置,例如rootController中。拿到了数据,接下来想做什么就依具体情况而定。

6.设备解绑

绑定了设备当然也就有解绑设备,例如在用户退出登录时,解绑设备一般只需要提供RegistrationID,调用后台提供的解绑接口传入RegistrationID就可以了。

7.关于IOS中的badge角标的注意点

这里只说一下关于如何清除角标(具体在哪里清除依项目而定,例如打开app或者读取消息之后),这时需要先调用window.JPush.resetBadge();清除服务器端的角标数,再调用window.JPush.setApplicationIconBadgeNumber(0);清除本地的角标数,具体参考github文档IOS部分,一般需要在cordova的ondeviceready和onresume事件中。并且前提是要进行平台判断,例如:

document.addEventListener("resume", function() {if ($window.isMobile.IOS() !== undefined && $window.isMobile.IOS() !== null) {//清空IOS中应用角标,服务器和本地

window.JPush.resetBadge();

window.JPush.setApplicationIconBadgeNumber(0);

}

},false);

需要注意的是如果只使用了window.JPush.setApplicationIconBadgeNumber(0);虽然每次可以清除角标,但是下次收到推送时角标数就会在上次基础上累加。所以必须先使用window.JPush.resetBadge();清除掉服务器端的角标数,这一点从极光的官方文档就显而易见。

20180110215742466620.png

原文:http://www.cnblogs.com/li-you/p/6374075.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 Android 使用极光推送绑定别名的代码示例: ```java import cn.jpush.android.api.JPushInterface; // 绑定别名 JPushInterface.setAlias(context, sequence, alias); // 解绑别名 JPushInterface.deleteAlias(context, sequence); ``` 其中,`context` 参数为当前上下文对象,`sequence` 参数为请求码,`alias` 参数为需要绑定的别名。 注意事项: - 绑定别名时,如果已经绑定了别名,则会覆盖之前的别名; - 解绑别名时,如果当前设备没有绑定别名,则不会有任何影响。 另外,在使用极光推送时,需要在 AndroidManifest.xml 文件中添加以下权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="cn.jpush.android.permission.RECEIVE_MSG" /> <uses-permission android:name="cn.jpush.android.permission.READ_PHONE_STATE" /> <uses-permission android:name="cn.jpush.android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="cn.jpush.android.permission.VIBRATE" /> <uses-permission android:name="cn.jpush.android.permission.RECEIVE_BOOT_COMPLETED" /> ``` 并且,需要在 AndroidManifest.xml 文件中添加以下服务和接收器: ```xml <!-- 极光推送服务 --> <service android:name="cn.jpush.android.service.PushService" android:enabled="true" android:exported="false" > <intent-filter> <action android:name="cn.jpush.android.intent.REGISTER" /> <action android:name="cn.jpush.android.intent.REPORT" /> <action android:name="cn.jpush.android.intent.PushService" /> <action android:name="cn.jpush.android.intent.PUSH_TIME" /> </intent-filter> </service> <!-- 极光推送接收器 --> <receiver android:name="cn.jpush.android.service.PushReceiver" android:enabled="true" > <intent-filter android:priority="1000"> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /> <category android:name="${applicationId}" /> </intent-filter> <intent-filter> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <category android:name="${applicationId}" /> </intent-filter> <intent-filter> <action android:name="cn.jpush.android.intent.UNREGISTRATION" /> <category android:name="${applicationId}" /> </intent-filter> <intent-filter> <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <category android:name="${applicationId}" /> <data android:scheme="package" /> </intent-filter> <intent-filter> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <category android:name="${applicationId}" /> </intent-filter> <intent-filter> <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <category android:name="${applicationId}" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.USER_PRESENT" /> <category android:name="${applicationId}" /> </intent-filter> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver> <!-- 极光推送唤醒接收器 --> <receiver android:name="cn.jpush.android.service.AlarmReceiver" android:enabled="true" /> ``` 以上是 Android 使用极光推送绑定别名的代码示例及注意事项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值