【问题标题】:Xamarin.Android: How to Add a Unique Intent For Each NotificationXamarin.Android:如何为每个通知添加唯一的 Intent
【发布时间】:2019-10-13 21:45:20
【问题描述】:

我将Intents 添加到Android.Support.V4.App.NotificationCompat.Builder 中,但Extras 没有被传递到OnNewIntent 的覆盖中,而且似乎参数总是相同的Intent。这个想法是用户通知列表中的foreach 通知我的应用程序创建一个自定义通知对象,然后创建一个Bundle 和一个Intent

//custom notification class just contains the information for notifications public class CustomNotification { public string _noteType { get; set; } public string _noteText { get; set; } public string _noteLink { get; set; } public int _noteIndex { get; set; } }

    public void SendNotifications(List<CustomNotification> notificationList)
    {
        try
        {
            var _ctx = Android.App.Application.Context;
            int _noteCount = 0;
            foreach (var note in notificationList)
            {
                //I'm instantiating a new Intent foreach so not sure why 
                // each would not have it's own Extras
                var resultIntent = new Intent(_ctx, typeof(MainActivity));
                var valuesForActivity = new Bundle();
                valuesForActivity.PutInt(MainActivity.COUNT_KEY, _count);
                //this will always be the same string when it hits OnNewIntent in MainActivity
                valuesForActivity.PutString("URL", note._noteLink);
                //this will always be the same number when it hits OnNewIntent in MainActivity
                valuesForActivity.PutInt("NoteNumber", _noteCount);
                resultIntent.PutExtras(valuesForActivity);

                var resultPendingIntent = PendingIntent.GetActivity(_ctx, 0, resultIntent, PendingIntentFlags.UpdateCurrent);

                resultIntent.AddFlags(ActivityFlags.SingleTop);

                // Build the notification:
                var builder = new Android.Support.V4.App.NotificationCompat.Builder(_ctx, MainActivity.CHANNEL_ID)
                              .SetAutoCancel(true) // Dismiss the notification from the notification area when the user clicks on it
                              .SetContentIntent(resultPendingIntent) // I'm passing the Intent here.. the rest of the builder vars work
                              .SetContentTitle(note._noteType) // Set the title
                              .SetNumber(_count) // Display the count in the Content Info
                              .SetSmallIcon(2130837590) // This is the icon to display
                              .SetContentText(note._noteText);


                MainActivity.NOTIFICATION_ID++;

                var notificationManager = Android.Support.V4.App.NotificationManagerCompat.From(_ctx);
                notificationManager.Notify(MainActivity.NOTIFICATION_ID, builder.Build());

                _noteCount++;
            }

        }
        catch
        {

        }
    }


    //this is inside MainActivity.cs
    protected override void OnNewIntent(Intent intent)
    {
        string url = "";
        int noteCount = 0;


        if (intent != null)
        {
            //this is always the same url
            url = intent.Extras.GetString("URL");
            //this is always the same int
            noteCount = intent.Extras.GetInt("NoteNumber");
        }

        try
        {
            switch (_viewPager.CurrentItem)
            {
                case 0:
                    _fm1.LoadCustomUrl(url);
                    break;
                case 1:
                    _fm2.LoadCustomUrl(url);
                    break;
                case 2:
                    _fm3.LoadCustomUrl(url);
                    break;
                case 3:
                    _fm4.LoadCustomUrl(url);
                    break;
                case 4:
                    _fm5.LoadCustomUrl(url);
                    break;
            }
        }
        catch
        {

        }
        base.OnNewIntent(intent);
    }

我希望当我将Intent 传递给构建器时,它会返回一个唯一值,但无论我点击哪个通知,都会返回相同的字符串。在构建每个通知时,我已经逐步完成了代码,并且一个唯一的字符串被传递到每个Intent。我在这里做错了什么?

【问题讨论】:

  • 可以吗?
  • 非常感谢您在这方面的帮助;昨晚我正准备进行测试,但还有另一个问题是通知没有正确解析。今晚我要弄清楚并再次测试!一旦我测试过,我一定会给你一个赞成票并标记为答案=]
  • @LeoZhu-MSFT 是的,这很有效......非常感谢!

标签: c# android-intent xamarin.android


【解决方案1】:

var resultPendingIntent = PendingIntent.GetActivity(_ctx, 0,resultIntent, PendingIntentFlags.UpdateCurrent);

Extra 会更新为最后传入 Intent 的 Extra,所以你会得到相同的 extra。如果您需要为每个通知获取正确的额外信息,有两种方法:

1.定义意图的时候,也需要区分意图!您可以在意图下添加代码,如下所示:

resultIntent.SetData(Android.Net.Uri.Parse("custom://" + SystemClock.CurrentThreadTimeMillis()));

2。 var resultPendingIntent = PendingIntent.GetActivity(_ctx, 0, resultIntent, PendingIntentFlags.UpdateCurrent); 改为:

var resultPendingIntent = PendingIntent.GetActivity(_ctx,  MainActivity.NOTIFICATION_ID, resultIntent, PendingIntentFlags.UpdateCurrent);

【讨论】:

  • 我根本没有使用方法一,但方法二效果很好。回想起来,这很有意义,不敢相信我没有注意到它。一个星期以来,我一直把头撞在墙上,哈哈!非常感谢你=]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
相关资源
最近更新 更多