如何通过单击推送通知来了解该应用程序是否已启动 [英] how to know if the app was launched by clicking on the push notification

查看:52
本文介绍了如何通过单击推送通知来了解该应用程序是否已启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一个标志和参数可以通过单击通知托盘中的推送通知来告诉我用户是否启动了活动/应用程序.

I want to know if there is a flag and parameter that can tell me if the user launched the activity/app by clicking on the push notification in the notification tray.

我在C2DMReceiver.java中的代码

My code in C2DMReceiver.java

Context context = getApplicationContext();

        PackageManager manager = context.getPackageManager();
        Intent notificationIntent = manager
                .getLaunchIntentForPackage(packageName);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        **notificationIntent.putExtra("fromRemote", true);**

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);

        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification();
        notification.icon = R.drawable.icon;
        notification.tickerText = message;
        notification.number = badge;
        notification.when = System.currentTimeMillis();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults = Notification.DEFAULT_ALL;
        notification.setLatestEventInfo(context, "Draw Something", message,
                pendingIntent);
        notification.contentIntent = pendingIntent;
        notificationManager.notify(0, notification);

我尝试设置

notificationIntent.putExtra("fromRemote", true);

但是启动该应用程序时,没有任何意图.

but when the app was launched there were no extras in the intent.

我的mainactivity的onCreate函数中的我的代码

my code in the onCreate function of my mainactivity

Bundle extras = getIntent().getExtras();
    if (extras != null) {
        print("onCreate - bundle has extras");
        for (String key: extras.keySet())
        {
          Log.v ("mytag", "MainActivity onCreate key =" + key);
        }
    }
    else {
        Log.v ("mytag", "onCreate - bundle has NO extras");
    }

我得到的输出是onCreate-捆绑包没有其他功能.因此,这些额外功能不会通过.

The output i get is onCreate - bundle has NO extras. So the extras are not getting passed through.

还有其他办法吗?在iOS中如此简单

So is there any other way?? It is so easy in iOS

推荐答案

只需发布答案,以便访问此页面的人都能获得解决方案.

Just posting an answer so that people who visit this page get the solution.

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

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.

传递的多余内容可以是任何东西,例如布尔值,字符串等.

The passed extra could be anything, a boolean, String etc.

来源- https://stackoverflow.com/a/7358692/3036759

这篇关于如何通过单击推送通知来了解该应用程序是否已启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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