如何确定Android App是否从Notification消息中打开?

Jir*_*ong 19 android push-notification

通常,当我在通知栏上有通知消息并单击它时.它打开该邮件的已注册应用程序.

在Startup的活动中,如何确定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);
Run Code Online (Sandbox Code Playgroud)

并在主要活动"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) {

        }
    }
Run Code Online (Sandbox Code Playgroud)

intent.getExtras()始终返回null.结果,我需要传递PendingIntent.FLAG_ONE_SHOT.它现在传递!!

Ovi*_*tcu 18

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

  • 请注意,除非您的新意图与最后使用的意图相比返回.equals()== false,否则将使用最后一个意图..equals中不考虑额外内容,因此您需要在setAction或类似设置中设置一些唯一标识符. (3认同)
  • 如果通知来自Google云怎么办???在我的情况下,getIntent()始终返回null。 (2认同)

Kho*_*aib 8

Start Activity代码就是这样的,否则一旦它来自GCM通知,从那时每次Activity都来自Recent list,它会说它来自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) {

    }
}
Run Code Online (Sandbox Code Playgroud)