【问题标题】:How to determine if Android App open from Notification message?如何确定Android App是否从通知消息中打开?
【发布时间】:2011-11-13 14:07:02
【问题描述】:

一般来说,当我在通知栏上有通知消息并单击它时。它打开该消息的注册应用程序。

在 Startup 的 Activity 上,如何判断 App 是否打开?

更好的是如何在 OnCreate() 方法上检索通知的 id?

更新:来自@Ovidiu - 这是我的 putExtra 推送代码

       Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());
       notification.contentView = contentView;

       Intent notificationIntent = new Intent(this, Startup.class);
       notificationIntent.putExtra("JOBID", jobId);

       PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);

       notification.flags = Notification.FLAG_AUTO_CANCEL;
       notification.contentIntent = contentIntent;


       mNotificationManager.notify(jobId, notification);

主活动“Startup.java”代码是

    Intent intent = this.getIntent();
    if (intent != null && intent.getExtras() != null && intent.getExtras().containsKey("JOBID")) {
        int jobID = this.getIntent().getExtras().getInt("JOBID");

        if (jobID > 0) {

        }
    }

intent.getExtras() 总是返回 null。结果,我需要通过 PendingIntent.FLAG_ONE_SHOT 。现在传了!!

【问题讨论】:

  • "需要通过 PendingIntent.FLAG_ONE_SHOT" - 真的很有帮助。但是您需要检查另一件事 - 如果此 Activity 来自历史记录。在下面检查我的答案。
  • FLAG_ONE_SHOT 导致应用程序在单击通知时仅启动一次,这可能不是您想要的,以防来自后台服务的粘性通知。在那种情况下,FLAG_UPDATE_CURRENT 对我有用。

标签: android push-notification


【解决方案1】:

还有一个案例。 当您通过 firebase 推送通知启动应用程序时,在额外的意图中有包含 { key : "from" , value : gcm_defaultSenderId } 的记录 这样,如果此记录可用,我就可以知道我的应用程序是由 firebase 推送通知启动的,否则我将自己的值通过 key 放入 PendingIntent extras 中:“from”

【讨论】:

    【解决方案2】:

    我有同样的问题,我不明白为什么我必须使用 putExtra 方法...... 所以我这样解决:当您收到通知并点击它时,应用程序将打开(通常它会打开应用程序主活动),并且在附加功能中,您可以找到有关该通知的一些信息。 您可以将键/值参数添加到您发送到注册设备的通知中。这些参数将被添加到 Intent Extras 中。

    因此您可以这样做:在您的通知中,添加一个表示您的通知 id 的参数。

    例如“messageId” -> “abc”,其中 abc 是您的通知标识符。

    然后,在您的主要活动中,您可以:

    if (getIntent().getExtras().keySet().contains("messageId")) {
        // you opened the app from a notification
        String messageId = getIntent().getStringExtra("messageId");
        // do domething...
    } else {
        // you opened the app normally
        // do domething...
    }
    

    然后您将检索通知的 id。 因此,您可以使用这些信息,例如,从您的数据库或其他操作中获取通知。

    【讨论】:

    • 您如何建议在不使用 putExtra 的情况下“向通知添加键/值参数”?
    【解决方案3】:

    Start Activity 的代码应该是这样的,否则一旦它来自 GCM 通知后,此后每次 Activity 来自最近列表时,它都会说它来自 GCM 通知,这是错误的。

    Intent intent = this.getIntent();
    if (intent != null && intent.getExtras() != null && intent.getExtras().containsKey("JOBID") && (intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
        int jobID = this.getIntent().getExtras().getInt("JOBID");
    
        if (jobID > 0) {
    
        }
    }
    

    【讨论】:

      【解决方案4】:

      您需要在创建Intent 时使用putExtra(ID_KEY,id) 来启动您的应用程序,并且在您的onCreate() 方法中您可以使用getIntent().getExtras().getInt(ID_KEY); 来检索您传递的ID integer。

      【讨论】:

      • 请注意,除非您的新意图与上次使用的意图相比返回 .equals() == false,否则将使用最后一个意图。 .equals 中不考虑额外内容,因此您需要在 setAction 或类似内容中设置一些唯一标识符。
      • 如果通知来自谷歌云怎么办??? getIntent() 在我的情况下总是返回 null。
      猜你喜欢
      • 1970-01-01
      • 2015-11-01
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      相关资源
      最近更新 更多