最近项目集成百度云推送,集成很简单但是用起来和其他推送有点不一样。
控制台推送和后台推送有点不一样。
******需要我们自己自定义行为:
先看看安卓推送通知格式;
Android通知格式
{ "title" : "hello" , "description": "hello world" //必选 "notification_builder_id": 0, //可选 "notification_basic_style": 7, //可选 "open_type":0, //可选 "url": "http://developer.baidu.com", //可选 "pkg_content":"", //可选 "custom_content":{"key":"value"}, }
属性说明:
- title:通知标题,可以为空;如果为空则设为appid对应的应用名。
- description:通知文本内容,不能为空。
- notification_builder_id:android客户端自定义通知样式,如果没有设置默认为0。
- notification_basic_style:只有notification_builder_id为0时有效,可以设置通知的基本样式包括(响铃:0x04;振动:0x02;可清除:0x01;),这是一个flag整形,每一位代表一种样式,如果想选择任意两种或三种通知样式,notification_basic_style的值即为对应样式数值相加后的值。
- open_type:点击通知后的行为(1:打开Url; 2:自定义行为;)。open_type =0,只回调onNotificationClicked方法,不做其他操作;open_type = 1,url != null:打开网页;open_type = 2,pkg_content = null:直接打开应用;open_type = 2,pkg_content != null:自定义动作打开应用。
- url:需要打开的Url地址,open_type为1时才有效。
- pkg_content:open_type为2时才有效,Android端SDK会把pkg_content字符串转换成Android Intent,通过该Intent打开对应app组件,所以pkg_content字符串格式必须遵循Intent uri格式,最简单的方法可以通过Intent方法toURI()获取。
- custom_content:自定义内容,键值对,Json对象形式(可选);在android客户端,这些键值对将以Intent中的extra进行传递。
第一步,在AndroidManifest.xml配置当用户点击要跳转的Activity,如下所示,注意intent-filter里面的内容,后面用得到。
<activity android:name=".HomeActivity" android:launchMode="singleTask" android:screenOrientation="portrait"> <intent-filter> <action android:name="com.push.HomeActivity" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
第二步,让服务器的人修改推送通知时候的两个参数的值
"open_type":2,
"pkg_content":“#Intent;action=com.push.HomeActivity;launchFlags=0x4000000;end”
",
open_type 值为2,pkg_content : 的值怎么获取呢?如下所示:
Intent intent = new Intent();
//在AndroidManifest.xml的activity里面intent-filter配置action
intent.setAction("
com.push.HomeActivity
");intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); String pkg_content = intent.toUri(Intent.FLAG_ACTIVITY_CLEAR_TOP);
这样服务器发送的通知当用户点击后就可以跳转到pkg_content配置的Activity啦。
@Override public void onNotificationClicked(Context context, String title, String description, String customContentString) { LogUtil.i(TAG, "dianjihailaima"); LogUtil.i(TAG, "dianjihailaima title = " + title); LogUtil.i(TAG, "dianjihailaima description = " + description); LogUtil.i(TAG, "dianjihailaima customContentString = " + customContentString); SharedPreferUtils.write("tui", "cub", "1"); }
其实customContentString = cunstom_content . 点击之后会走你指定跳转的Activity对应的onResume().这里可以进行状态判断。
custom_content需要后台定义,放在通知格式里面,不然为空
,可以通过这个玩意来进行相关页面的操作
集成推送的时候,把app的key以及 secret給后台,注册推送的时候再根据后台传channelId以及userId。看你们的需求再定。