Android集成华为推送的问题总结
来源:互联网 发布:中国电信云计算公司 编辑:程序博客网 时间:2024/06/04 00:25
前言
公司最近想要把几个主流平台的推送进行一下整合,做一个DEMO出来,方便之后使用.做安卓开发的童鞋应该也都应该了解,自定义的服务基本上在APP被杀掉后也会被干掉(没有设置后台保护的情况下),所以要保证推送的及时和不失真,最好的方法还是去各大主流的手机厂商开发者网站去注册申请推送服务.
总结
关于集成华为推送的部分这里就不做介绍了.主要对集成华为推送服务后遇到的问题进行记录总结.希望可以帮助到同样遇到问题的童鞋.
总结一:
华为推送的初始化依赖Activity,所以不能在Application中进行初始化.
总结二:
华为推送服务中提供的透传消息是可能会丢失的.(当APP被杀死后,透传消息有时是收不到的,并且在重新启动APP后,透传消息也是收不到的,所以不推荐使用透传去做一些重要的功能)
总结三:
先看一下官方的Recevier:
public class HuaweiPushReceiver extends PushReceiver { private static final String TAG = "Huawei PushReceiver"; /** * 连接上华为服务时会调用,可以获取token值 * * @param context * @param token * @param extras */ @Override public void onToken(Context context, String token, Bundle extras) { String belongId = extras.getString("belongId"); String content = "get token and belongId successful, token = " + token + ",belongId = " + belongId; Log.e(TAG, content); } /** * 透传消息的回调方法 * * @param context * @param msg * @param bundle * @return */ @Override public boolean onPushMsg(Context context, byte[] msg, Bundle bundle) { try { String content = "-------Receive a Push pass-by message: " + new String(msg, "UTF-8"); Log.e(TAG, content); } catch (Exception e) { e.printStackTrace(); } return false; } /** * 自定义的消息的回调方法 * * @param context * @param event * @param extras */ @Override public void onEvent(Context context, PushReceiver.Event event, Bundle extras) { PushManager.getInstance().notifyPush(extras.getString(BOUND_KEY.pushMsgKey)); if (Event.NOTIFICATION_OPENED.equals(event) || Event.NOTIFICATION_CLICK_BTN.equals(event)) { int notifyId = extras.getInt(BOUND_KEY.pushNotifyId, 0); if (0 != notifyId) { NotificationManager manager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); manager.cancel(notifyId); } String content = "--------receive extented notification message: " + extras.getString (BOUND_KEY.pushMsgKey); Log.e(TAG, content); } super.onEvent(context, event, extras); } /** * 连接状态的回调方法 * * @param context * @param pushState */ @Override public void onPushState(Context context, boolean pushState) { try { String content = "---------The current push status: " + (pushState ? "Connected" : "Disconnected"); Log.e(TAG, content); } catch (Exception e) { e.printStackTrace(); } }}
对消息的监听,提供了4种回调方法,注释写得很清楚,官网也有相关的解释,不多说.这里说一下onEvent方法,当时试了很多次没试出这个回调如何触发(可能是自己粗心大意),这里记录下,希望能帮到同样粗心大意的童鞋吧.看下图
注意右边的添加按钮.在填完键和值之后一定要添加上!不然是没有效果的.还有就是推送消息收到后,只有点击这条消息的时候才会走onEvent方法.
总结四:
当APP接收到推送后,点击消息,会发现总是会先打开启动页,这个问题要怎么解决呢?翻了翻文档,仔细看了一下后台发送推送时页面,会看到如下内容:
选择直接打开程序的话会走一遍启动页.所以只能来研究一下这个自定义动作了,但是不知道填什么,这里会有一个例子,不知道有多少童鞋看不懂哈,反正开始我没看明白(其实这里边用到了Content Provider方面的知识,项目中用到的比较少,所以有一些遗忘了,希望同样没看明白的童鞋可以去温习一下).这里直接写一下步骤,首先在AndroidManifest中需要跳转的Activity中加入
<!-- 需要跳转到的页面--> <activity android:name=".demo.activity.PushDemoActivity" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <data android:host="com.myhost.push" android:path="/push_detail" android:scheme="myscheme"/> </intent-filter> </activity>
之后在Activty中打印出来需要的uri就可以了,如下
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("myscheme://com.myhost.push/push_detail?message=what")); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); String intentUri = intent.toUri(Intent.URI_INTENT_SCHEME); Log.e("PushDemoActivity","action是:" + intentUri);
这个得到的intentUri就是需要填到自定义动作中的内容,大概样子是
intent://com.myhost.push/push_detailmessage=what#Intent;scheme=myscheme;action=android.intent.action.VIEW;launchFlags=0x10000000;end
当然scheme,host这些可以根据项目的功能模块去修改名字,这里只是举个例子.
- Android集成华为推送的问题总结
- Android 集成华为推送
- Android集成小米华为推送以及收不到离线消息的坑
- android集成友盟推送遇到的问题
- Android 极光推送的集成
- 关于Android studio 使用极光推送,集成成功,但是推送不成功的问题
- android接入华为推送
- 集成极光推送总结
- android studio极光推送的集成
- Android 集成极光推送的第三方
- Android集成极光推送 的异常
- Android应用的信鸽推送集成
- Android JPush的集成 极光推送
- 友盟推送集成小米华为通道
- Android 极光推送集成
- android极光推送集成
- Android集成极光推送
- android集成极光推送
- Hashmap为什么容量是2的幂次,什么是负载因子
- asa文件导入到mysql
- webstorm(10.0.2)的端口号修改
- postman加密请求接口
- 线段树区间更新 延时标记
- Android集成华为推送的问题总结
- That darn "libtoolize: AC_CONFIG_MACRO_DIR([m4]) conflicts with ACLOCAL_AMFLAGS=-I m4" error
- perl Email::Sender 发邮件
- 三分查找
- UNIX再学习 -- 信号
- tomcat使用log4j打印应用日志
- 网络中的各层协议
- Flume中的TaildirSource
- radioGroup+fragment的显示和隐藏