推送通知有3种情况。
- 情况1:应用程序已在运行,通知出现。
- 情况2:应用程序关闭,但通过单击应用程序图标打开通知。
- 情况3:应用程序关闭,并从通知单击打开应用程序
我的问题是如何检测应用程序是从情况2还是情况3打开的?如果我能够检测到,那么我就可以保存一些值在首选项中,并使用该值来区分是否需要打开主活动或通知活动。
如果您有更好的想法来决定启动闪屏后应打开哪个活动(无论是主要活动还是通知活动),请告诉我。
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);
在目标(闪屏)活动中。
boolean pushClicked = false;
if(getIntent()!=null){
pushClicked = getIntent().getStringExtra("pushClicked");
System.out.println("pushClicked="+pushClicked);
}
System.out.println(pushClicked );
始终返回false
onCreate
中检查该标志。 - Iamat8