1 问题
推送通知打开指定Activity时如何操作?
2 解决方案
打开指定Activity主要有两种方式。
2.1 使用SDK打开Activity
移动推送Android SDK是支持打开Activity功能的,此时只需要在推送时指定通知的打开方式即可。相关OpenApi代码可参考:
- [backcolor=transparent]PushRequest[backcolor=transparent] pushRequest [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]PushRequest[backcolor=transparent]();
- [backcolor=transparent]// 其余设置省略...
- [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setPushType[backcolor=transparent]([backcolor=transparent]"NOTICE"[backcolor=transparent]);
- [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" : 无跳转
- [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setAndroidActivity[backcolor=transparent]([backcolor=transparent]"您需要打开的界面"[backcolor=transparent]);
2.2 自行打开Activity
如果SDK自带的打开activity功能无法满足您的需求,您可以在通知点击回调中自行完成activity跳转逻辑,可参考如下方案。首先,将通知点击动作设置为NONE:
- [backcolor=transparent]PushRequest[backcolor=transparent] pushRequest [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]PushRequest[backcolor=transparent]();
- [backcolor=transparent]// 其余设置省略...
- [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setPushType[backcolor=transparent]([backcolor=transparent]"NOTICE"[backcolor=transparent]);
- [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跳转逻辑:
- [backcolor=transparent] [backcolor=transparent]@Override
- [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]{
- [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]);
- [backcolor=transparent] [backcolor=transparent]...
- [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]);
- [backcolor=transparent] [backcolor=transparent]....
- [backcolor=transparent] intent[backcolor=transparent].[backcolor=transparent]putExtra[backcolor=transparent]([backcolor=transparent]"title"[backcolor=transparent],[backcolor=transparent]title[backcolor=transparent]);
- [backcolor=transparent] intent[backcolor=transparent].[backcolor=transparent]putExtra[backcolor=transparent]([backcolor=transparent]"summary"[backcolor=transparent],[backcolor=transparent]summary[backcolor=transparent]);
- [backcolor=transparent] intent[backcolor=transparent].[backcolor=transparent]setFlags[backcolor=transparent]([backcolor=transparent]Intent[backcolor=transparent].[backcolor=transparent]FLAG_ACTIVITY_NEW_TASK[backcolor=transparent]);
- [backcolor=transparent] context[backcolor=transparent].[backcolor=transparent]startActivity[backcolor=transparent]([backcolor=transparent]intent[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
[font=PingFangSC, "]注意:由于BroadcastReceiver中获取到的Context不是ActivityContenxt而是ApplicationContext,所以无法直接打开activity,而需要将对应intent设置为Intent.FLAG_ACTIVITY_NEW_TASK才行。