未为Launcher应用程序设置Pending Intent Extras

时间:2017-09-13 05:59:18

标签: c# android xamarin.android android-pendingintent android-launcher

我创建了一个名为HomeActivity的Launcher应用程序,其中包含LaunchMode = SingleTaskCATEGORY_HOME以及CATEGORY_LAUNCHER。此活动反过来也会启动新活动和其他一些应用程序。

该活动与Firebase Messaging服务相关联,以获取推送通知。 Firebase服务添加了一些额外内容(从推送通知中收集并在HomeActivity

中的附加内容中发送
public override void OnMessageReceived(RemoteMessage message)
{
    Notification.Builder builder = new Notification.Builder(this);
    builder.SetSmallIcon(Resource.Drawable.Icon);
    Intent intent = new Intent(this, typeof(HomeActivity));
    intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);    
    intent.PutExtra(General.UPDATE_NOTIFIED, true);
    if(message.Data != null)
    {
        if (message.Data.Any())
        {
            foreach(KeyValuePair<string,string> kv in message.Data)
            {
                intent.PutExtra(kv.Key, kv.Value);
            }
        }
    }
    PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent);
    builder.SetContentIntent(pendingIntent);
    builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.app_logo));
    builder.SetContentTitle(message.GetNotification().Title);
    builder.SetContentText(message.GetNotification().Body);
    builder.SetDefaults(NotificationDefaults.Sound);
    builder.SetAutoCancel(true);
    NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
    notificationManager.Notify(1, builder.Build());
}

每当HomeActivity位于首位并收到并点击推送通知时,我就可以访问HomeActivity的{​​{1}}功能中的附加内容(我已覆盖OnResume )。

OnNewIntent(Intent intent)

但是当我处于由protected override void OnNewIntent(Intent intent) { bool updateNotification = intent.GetBooleanExtra(General.UPDATE_NOTIFIED, false); //Check if Extra is set this.Intent = intent; base.OnNewIntent(intent); } 启动的另一个活动中时,点击了推送通知;该应用确实返回HomeActivity,但HomeActivity中没有其他内容。

我尝试了各种各样的旗帜,包括但不限于IntentNewTask

我仍然无法理解为什么在另一项活动到位时点击通知时未设置额外内容。我在这里错过了什么。

1 个答案:

答案 0 :(得分:0)

好的,我弄清楚了问题是什么。

如果您发布带有数据的通知(即显示消息),Firebase系统将不会触发OnMessageReceived方法。

需要做的是推送通知中只有data个有效负载将触发负责设置数据的OnMessageRecieved函数。

所以通知应该像

{
    to: "topic | Registered device",
    data:{
          data1: "data_value1",
          data2: "data_value2",
          data3: "data_value3",
    }
}