从通知中检测应用激活
Detecting app activation from notification
我有一个使用 GCM 推送通知的应用程序。我已经收到消息并在应用程序打开时正确操作。问题是我需要应用程序在关闭时打开通知上的特定文章。
如何检查应用程序是从通知而不是启动器打开的,以及如何获取所述通知数据?
check if the app was opened from a notification and not the launcher
您可以使用PendingIntent.GetActivity()来实现这个功能。当您在 Notification
中添加此方法时,它将检索一个 PendingIntent
,这将启动一个新的 Activity
。这是一个基本示例:
//Set the activity, it will be opened when click the notification
var intent = new Intent(context, typeof(NotificationActivity));
intent.AddFlags(ActivityFlags.ClearTop);
intent.PutExtra("name", "York");
intent.PutExtra("Id", 1234);
var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.OneShot);
NotificationManager notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
var notificationBuilder = new NotificationCompat.Builder(context)
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentTitle("FCM Message")
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
notificationManager.Notify(0, notificationBuilder.Build());
how to get said notification data?
如果使用Intent
传递数据如下:
intent.PutExtra("name", "York");
intent.PutExtra("Id", 1234);
当点击notification
打开Activity
时,您可以这样恢复数据:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
if (Intent != null)
{
string name = Intent.GetStringExtra("name");
int id = Intent.GetIntExtra("Id", 0);
}
...
}
我有一个使用 GCM 推送通知的应用程序。我已经收到消息并在应用程序打开时正确操作。问题是我需要应用程序在关闭时打开通知上的特定文章。
如何检查应用程序是从通知而不是启动器打开的,以及如何获取所述通知数据?
check if the app was opened from a notification and not the launcher
您可以使用PendingIntent.GetActivity()来实现这个功能。当您在 Notification
中添加此方法时,它将检索一个 PendingIntent
,这将启动一个新的 Activity
。这是一个基本示例:
//Set the activity, it will be opened when click the notification
var intent = new Intent(context, typeof(NotificationActivity));
intent.AddFlags(ActivityFlags.ClearTop);
intent.PutExtra("name", "York");
intent.PutExtra("Id", 1234);
var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.OneShot);
NotificationManager notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
var notificationBuilder = new NotificationCompat.Builder(context)
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentTitle("FCM Message")
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
notificationManager.Notify(0, notificationBuilder.Build());
how to get said notification data?
如果使用Intent
传递数据如下:
intent.PutExtra("name", "York");
intent.PutExtra("Id", 1234);
当点击notification
打开Activity
时,您可以这样恢复数据:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
if (Intent != null)
{
string name = Intent.GetStringExtra("name");
int id = Intent.GetIntExtra("Id", 0);
}
...
}