推送通知中有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 );
总是假的
答案 0 :(得分:3)
添加额外的布尔值以及为在通知接收器中打开应用程序活动而创建的意图。 例如:
@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中的此布尔值,以区分推送点击和应用程序图标点击。