Push Notification 中有 3 种情况。
- 案例 1:应用已在运行并出现通知。
- 案例 2:应用已关闭并出现通知,但应用是通过点击应用图标打开的
- 案例 3:应用关闭,应用通过通知点击打开
我的问题是如何检测应用程序是从案例 2 还是案例 3 打开的?如果我能够检测到我可以优先保存一些值并使用该值我可以区分我是否必须打开主 Activity 或通知 Activity 。
如果您有更好的想法来决定启动后应该打开哪个 Activity (主 Activity 或通知 Activity ),请告诉我。
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("My Notification")
.setContentText("You have a received notification.")
.setSmallIcon(getNotificationIcon())
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_launcher))
.build();
notification.defaults=Notification.DEFAULT_SOUND;
notification.number = notificationCount++;
Intent notificationIntent = new Intent(context, SplashActivity.class);
notificationIntent.putExtra("pushClicked", true);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
System.out.println("title="+title+"message="+message);
notification.setLatestEventInfo(context, title, message, contentIntent);
int SERVER_DATA_RECEIVED = 1;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(SERVER_DATA_RECEIVED, notification);
在 Target(Splash) Activity 中
boolean pushClicked = false;
if(getIntent()!=null){
pushClicked = getIntent().getStringExtra("pushClicked");
System.out.println("pushClicked="+pushClicked);
}
System.out.println(pushClicked );
总是报错
最佳答案
添加一个额外的 bool 值以及为在通知接收器中打开应用程序 Activity 而创建的 Intent 。 例如:
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null)
return;
Intent splashIntent = new Intent(context, TargetActivity.class);
splashIntent.putExtra("pushClicked", true);
context.startActivity(splashIntent);
}
在 TargetActivity 中检查此 bool 值以区分推送点击和应用程序图标点击。
关于android - 如何检测应用程序是使用应用程序图标点击打开还是从通知点击打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34588252/