开发者社区> 问答> 正文

移动推送Android SDK:推送通知打开指定Activity时如何操作?



1 问题


推送通知打开指定Activity时如何操作?

2 解决方案


打开指定Activity主要有两种方式。

2.1 使用SDK打开Activity


移动推送Android SDK是支持打开Activity功能的,此时只需要在推送时指定通知的打开方式即可。相关OpenApi代码可参考:

  1. [backcolor=transparent]PushRequest[backcolor=transparent] pushRequest [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]PushRequest[backcolor=transparent]();
  2. [backcolor=transparent]// 其余设置省略...
  3. [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setPushType[backcolor=transparent]([backcolor=transparent]"NOTICE"[backcolor=transparent]);
  4. [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setAndroidOpenType[backcolor=transparent]([backcolor=transparent]"ACTIVITY"[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]//点击通知后动作 "APPLICATION" : 打开应用 "ACTIVITY" : 打开AndroidActivity "URL" : 打开URL "NONE" : 无跳转
  5. [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setAndroidActivity[backcolor=transparent]([backcolor=transparent]"您需要打开的界面"[backcolor=transparent]);


2.2 自行打开Activity


如果SDK自带的打开activity功能无法满足您的需求,您可以在通知点击回调中自行完成activity跳转逻辑,可参考如下方案。首先,将通知点击动作设置为NONE:
  1. [backcolor=transparent]PushRequest[backcolor=transparent] pushRequest [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]PushRequest[backcolor=transparent]();
  2. [backcolor=transparent]// 其余设置省略...
  3. [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setPushType[backcolor=transparent]([backcolor=transparent]"NOTICE"[backcolor=transparent]);
  4. [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setAndroidOpenType[backcolor=transparent]([backcolor=transparent]"NONE"[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]//点击通知后动作 "APPLICATION" : 打开应用 "ACTIVITY" : 打开AndroidActivity "URL" : 打开URL "NONE" : 无跳转

然后在通知点击回调onNotificationOpened中完成activity跳转逻辑:
  1. [backcolor=transparent]    [backcolor=transparent]@Override
  2. [backcolor=transparent]    [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] onNotificationOpened[backcolor=transparent]([backcolor=transparent]Context[backcolor=transparent] context[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] title[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] summary[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] extraMap[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  3. [backcolor=transparent]        [backcolor=transparent]Log[backcolor=transparent].[backcolor=transparent]e[backcolor=transparent]([backcolor=transparent]"MyMessageReceiver"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"onNotificationOpened, title: "[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] title [backcolor=transparent]+[backcolor=transparent] [backcolor=transparent]", summary: "[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] summary [backcolor=transparent]+[backcolor=transparent] [backcolor=transparent]", extraMap:"[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] extraMap[backcolor=transparent]);
  4. [backcolor=transparent]        [backcolor=transparent]...
  5. [backcolor=transparent]        [backcolor=transparent]Intent[backcolor=transparent] intent [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]Intent[backcolor=transparent]([backcolor=transparent]context[backcolor=transparent],[backcolor=transparent]"您需要打开的界面"[backcolor=transparent]);
  6. [backcolor=transparent]        [backcolor=transparent]....
  7. [backcolor=transparent]        intent[backcolor=transparent].[backcolor=transparent]putExtra[backcolor=transparent]([backcolor=transparent]"title"[backcolor=transparent],[backcolor=transparent]title[backcolor=transparent]);
  8. [backcolor=transparent]        intent[backcolor=transparent].[backcolor=transparent]putExtra[backcolor=transparent]([backcolor=transparent]"summary"[backcolor=transparent],[backcolor=transparent]summary[backcolor=transparent]);
  9. [backcolor=transparent]        intent[backcolor=transparent].[backcolor=transparent]setFlags[backcolor=transparent]([backcolor=transparent]Intent[backcolor=transparent].[backcolor=transparent]FLAG_ACTIVITY_NEW_TASK[backcolor=transparent]);
  10. [backcolor=transparent]        context[backcolor=transparent].[backcolor=transparent]startActivity[backcolor=transparent]([backcolor=transparent]intent[backcolor=transparent]);
  11. [backcolor=transparent]    [backcolor=transparent]}
[font=PingFangSC, "]注意:由于BroadcastReceiver中获取到的Context不是ActivityContenxt而是ApplicationContext,所以无法直接打开activity,而需要将对应intent设置为Intent.FLAG_ACTIVITY_NEW_TASK才行。

展开
收起
猫饭先生 2017-10-23 14:21:08 1504 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载