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

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

问题描述

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

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 应用程序是否打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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