简体   繁体   English

Xamarin.Android:如何为每个通知添加唯一的意图

[英]Xamarin.Android: How to Add a Unique Intent For Each Notification

I am adding Intent s into a Android.Support.V4.App.NotificationCompat.Builder but the Extras are not getting passed into an override of OnNewIntent and it seems that the parameter is always the same Intent .我将Intent添加到Android.Support.V4.App.NotificationCompat.BuilderExtras没有被传递到OnNewIntent的覆盖中,并且参数似乎始终是相同的Intent The idea is foreach notification in the user's notification list my app creates a custom notification object and then creates a Bundle and an Intent .这个想法是用户通知列表中的foreach通知我的应用程序创建一个自定义通知 object 然后创建一个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);
    }

I would expect that when I pass the Intent into the builder it would return a unique value but the same string always returns no matter which notification I click.我希望当我将Intent传递给构建器时,它会返回一个唯一值,但无论我单击哪个通知,都会返回相同的字符串。 I've stepped through the code as each notification is built and a unique string is being passed into each Intent .在构建每个通知并且将唯一字符串传递到每个Intent时,我已经逐步完成了代码。 What did I do wrong here?我在这里做错了什么?

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

Extra will be updated to the Extra of the last incoming Intent,so you would get the same extra. Extra 将更新为最后传入 Intent 的 Extra,因此您将获得相同的 extra。 If you need to get the correct extra for each notification,there are two methods:如果您需要为每个通知获取正确的额外信息,有两种方法:

1.When you define intents, you also need to distinguish between intents, You could add the code under the intent: like this: 1.定义intent的时候,还需要区分intent,可以在intent下面添加代码:像这样:

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

2. var resultPendingIntent = PendingIntent.GetActivity(_ctx, 0, resultIntent, PendingIntentFlags.UpdateCurrent); 2. var resultPendingIntent = PendingIntent.GetActivity(_ctx, 0, resultIntent, PendingIntentFlags.UpdateCurrent); change to:改成:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM