您的位置:首页 > 移动开发 > Android开发

(浅谈)Jpush推送通知点击跳转到具体界面

2016-07-12 15:25 381 查看

(好记性,不如烂笔头)

    Android端接入Jpush推送后,遇见的一个小问题:“假如有多条推送的通知推送过来后,点击任意一条所获得的Intent传输所需数据,都变成了最后一条通知的数据”。网上对这块貌似没有好的解决方案,下面解决方案给大家献丑一下,以备不时之需。

if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Map<String,String> map = new ArrayMap<String,String>();
for (String key : bundle.keySet()) {
if (key.equals(JPushInterface.EXTRA_EXTRA)) {
if (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {
continue;
}
try {
JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
Iterator<String> it =  json.keys();
while (it.hasNext()) {
String myKey = it.next().toString();
map.put(myKey, json.getString(myKey));
}

} catch (JSONException e) {
}

}
}

Bundle bundle = new Bundle();
bundle.putString("taskId" , map.get("taskId"));
if("1".equals(map.get("type"))){
Intent i = new Intent(context, 跳转的Activity);
i.putExtras(bundle);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}else if("2".equals(map.get("type"))){
Intent i = new Intent(context, 跳转的Activity);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtras(bundle);
context.startActivity(i);
}

}
这里示例是根据json数据获取“type”和“taskId”两个字段,并根据type类型跳转到具体界面,传输相应的taskId。看起来很高大上的实现,通过迭代器和集合两个重点技术,这是我同事的解决方案,在我面前得瑟后,我感觉有问题,这种方法完全就是多余。我们可能会模糊JPush官方文档的概念:

public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Log.d(TAG, "onReceive - " + intent.getAction());

if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
}else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
System.out.println("收到了自定义消息。消息内容是:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
// 自定义消息不会展示在通知栏,完全要开发者写代码去处理
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
System.out.println("收到了通知");
// 在这里可以做些统计,或者做些其他工作
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
System.out.println("用户点击打开了通知");
// 在这里可以自己写代码去定义用户点击后的行为
Intent i = new Intent(context, TestActivity.class);  //自定义打开的界面
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
} else {
Log.d(TAG, "Unhandled intent - " + intent.getAction());
}
}
傻瓜式思维可能导致我们以为只有在判断getAction为收到通知时才能获取数据的僵局,其实JPush的技术大神们早就在我们进行点击后,会将具体数据传输给我们来处理,这不同于IOS的APNs机制,只是一个通知而已。所以,优化后的就是:

if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Bundle bundle = intent.getExtras();
String s = bundle.getString(JPushInterface.EXTRA_EXTRA);
try {
JSONObject obj = new JSONObject(s);
taskId = obj.getString("taskId");
type = obj.getString("type");
}catch(JSONException e){

}
// 在这里可以自己写代码去定义用户点击后的行为
Bundle bundle2 = new Bundle();
bundle2.putString("taskId" , taskId);
if("1".equals(type)){
Intent i = new Intent(context, VideoTaskDetailActivity.class);  //自定义打开的界面
i.putExtras(bundle2);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}else if("2".equals(type)){
Intent i = new Intent(context, TaskDetailActivity.class);  //自定义打开的界面
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtras(bundle2);
context.startActivity(i);
}

}

定义的全局变量taskId和type是问题出现的关键,总结起来就一句话:在
if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction()))
判断处理所有逻辑就行了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息