[英]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.