简体   繁体   English

如何通过点击推送通知来了解应用程序是否已启动

[英]how to know if the app was launched by clicking on the push notification

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. 我想知道是否有一个标志和参数可以告诉我用户是否通过点击通知托盘中的推送通知启动了活动/应用程序。

My code in C2DMReceiver.java 我在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);

I tried setting 我尝试过设置

notificationIntent.putExtra("fromRemote", true);

but when the app was launched there were no extras in the intent. 但是当应用程序启动时,意图中没有额外的内容。

my code in the onCreate function of my mainactivity 我的代码在我的mainactivity的onCreate函数中

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");
    }

The output i get is onCreate - bundle has NO extras. 我得到的输出是onCreate - bundle没有额外的东西。 So the extras are not getting passed through. 所以附加内容没有通过。

So is there any other way?? 还有其他方法吗? It is so easy in iOS 它在iOS中非常容易

Just posting an answer so that people who visit this page get the solution. 只需发布答案,以便访问此页面的人获得解决方案。

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); 在创建启动应用程序的Intent时需要使用putExtra(ID_KEY,id) ,在onCreate()方法中可以使用getIntent().getExtras().getInt(ID_KEY); to retrieve your passed id integer. 检索传递的id整数。

The passed extra could be anything, a boolean, String etc. 传递的额外可以是任何东西,布尔值,字符串等。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道是否通过Android上的通知启动了应用/游戏? - How to know if app/game was launched via notification on Android? android:如何在点击推送通知时打开应用 - android:how to open the app on clicking on push notification 是否可以通过单击链接知道应用程序已启动? - Is it possible to know that an app was launched by clicking on a link? 用户单击通知启动后台应用程序时如何获取Android推送通知有效负载 - How to get Android push notification payload when background App is launched by user click on notification 单击推送通知如何在应用程序中打开特定页面? - How to open specific page in the app on clicking Push Notification? Android:如何知道是否可以启动应用 - Android: How to know if an app can be launched 如何在单击推送通知时打开特定活动 - How to open a specific activity on clicking push notification 如何不通过点击通知打开应用程序? - how not to open the app by clicking on the notification? 如何知道我们的应用是否由其他应用的Intent启动 - How to know if our app is launched by Intent of other App 使用PhoneGap插件点击推送通知时无法打开应用程序? - Unable to open the app when clicking on push notification using PhoneGap plugin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM