android - 如何检测应用程序是使用应用程序图标点击打开还是从通知点击打开?

标签 android push-notification

Push Notification 中有 3 种情况。

  1. 案例 1:应用已在运行并出现通知。
  2. 案例 2:应用已关闭并出现通知,但应用是通过点击应用图标打开的
  3. 案例 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/

相关文章:

java - Android - 云保存不起作用

java - 使用带有 AbstractThreadedSyncAdapter 的 RoboGuice 上下文注入(inject)

android - 在 3G 网络中使用 Retrofit 时流被重置为 : PROTOCOL_ERROR,

java - 在此处 map 上添加 SVG 图像

ios - 设备 ID 未存储在服务器 IOS7 中

不使用 GCM 的 Android 推送通知

java - Fragment 和 ListFragment 不兼容

php - 如何使用 PHP cURL 和 FCM 向移动设备发送通知?

php - 使用 PHP 通过 OneSignal 发送推送通知

ios - 控制显示哪些 iOS 通知