如何确定Android App是否从通知消息中打开? [英] How to determine if Android App open from Notification message?

查看:424
本文介绍了如何确定Android App是否从通知消息中打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In general, when i have notification message on the notification bar and click on it. It open the registered App for that message.

在Startup的Activity上,如何确定是否已从中打开App?

On Startup's Activity, How to determine if App is open from it?

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

and more better is How to retrieve the notification's id on the OnCreate() method?

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

Update: from @Ovidiu - here is my code to putExtra to push

       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"代码为

and on Main Activity "Startup.java" code is

    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.现在就过去了!!

intent.getExtras() always return null. Turn out, I need to pass PendingIntent.FLAG_ONE_SHOT . It is now passed along!!

推荐答案

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

You need to use putExtra(ID_KEY,id) when you create your Intent for starting your application, and in your onCreate() method you can use getIntent().getExtras().getInt(ID_KEY); to retrieve your passed id integer.

这篇关于如何确定Android App是否从通知消息中打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆