当在xamarin.forms中单击包含多个本地通知的推送通知时,总是返回相同的ID

问题描述 投票:0回答:1

我正在为我的应用程序使用xamarin.forms。我在应用程序上收到多个本地通知。当我单击通知时,它将根据ID转到特定页面。我通过未定的意图发送ID。但是,当我尝试发送多个通知时,pendingintent仅存储了最后一个ID。请查看我正在使用的以下代码:

MyFirebaseMessagingService.cs

using Android.App;
using Android.Content;
using Firebase.Messaging;
using Android.Graphics;
using System;
using Android.Support.V4.App;
using System.Text.RegularExpressions;
using Microsoft.AppCenter.Crashes;
using Android.Widget;
using Android.OS;
using System.Threading.Tasks;
using Android.Support.V4.Content;
using MyPrj.Helper;

namespace MyPrj.Droid.Services
{
    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
        const string TAG = "MyFirebaseMsgService";
        public const string PRIMARY_CHANNEL = "default";


        /**
         * Called when message is received.
         */


        public override void HandleIntent(Intent p0)
        {
            try
            {
                base.HandleIntent(p0);
                var tripid = p0.Extras.GetString("key2");
                if (!string.IsNullOrEmpty(tripid))
                {
                    CreateNotification(p0);
                }
            }
            catch (System.Exception ex)
            {
                Crashes.TrackError(ex);
            }
        }

        private void CreateNotification(Intent p0)
        {
            try
            {
                NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);

                var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
                int id = new Random(seed).Next(000000000, 999999999);

                string title = "";
                string body = "";

                var intent = new Intent(this, typeof(MainActivity));

                var i = p0 as Intent;
                var bundle = i.Extras;
                title = bundle.GetString("gcm.notification.title");
                body = bundle.GetString("gcm.notification.body");
                var tripid = bundle.GetString("key2");

                intent.PutExtra("title", title);
                intent.PutExtra("body", body);
                intent.PutExtra("tripid", tripid);
                intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
                var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);


                NotificationCompat.Builder notification;
                if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                {
                    var chan1 = new NotificationChannel(PRIMARY_CHANNEL,
                       new Java.Lang.String("Primary"), NotificationImportance.High);
                    chan1.LightColor = Color.Green;

                    manager.CreateNotificationChannel(chan1);

                    notification = new NotificationCompat.Builder(this, PRIMARY_CHANNEL).SetOngoing(true);
                }
                else
                {
                    notification = new NotificationCompat.Builder(this);
                }

                notification.SetContentIntent(pendingIntent)
                         .SetContentTitle(title)
                         .SetContentText(body)
                         .SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.icon))
                         .SetSmallIcon(Resource.Drawable.icon_transparent)
                         .SetStyle((new NotificationCompat.BigTextStyle()))
                         .SetPriority(NotificationCompat.PriorityHigh)
                         .SetColor(0x9c6114)
                         .SetAutoCancel(true)
                         .SetOngoing(true);

                manager.Notify(Convert.ToInt32(tripid), notification.Build());
            }
            catch (System.Exception ex)
            {
                Crashes.TrackError(ex);
            }

        }
    }
}

MainActivity.cs:

protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            var title = intent.GetStringExtra("title");
            var tripid = intent.GetStringExtra("tripid");
            if (title != null)
            {
                if (!string.IsNullOrEmpty(tripid))
                {
                    int tripidnotification = Convert.ToInt32(tripid);
                    TruckDetails page = new TruckDetails(tripidnotification);
                    Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(page);
                }
            }
        }

当我单击第二个通知时,MainActivity类的OnNewIntent尚未调用。

[请帮助我解决此问题。

xamarin.forms xamarin.android
1个回答
0
投票

Extra将被更新为最后一个传入Intent的Extra,因此您将获得相同的Extra。如果您需要为每个通知获取正确的附加信息,则有两种方法:

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

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

2。 var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);更改为:

var pendingIntent = PendingIntent.GetActivity(_ctx,Convert.ToInt32(tripid), resultIntent, PendingIntentFlags.UpdateCurrent);
© www.soinside.com 2019 - 2024. All rights reserved.