xamarin 有没有办法知道推送通知无法发出?

qeeaahzv  于 2023-05-15  发布在  其他
关注(0)|答案(1)|浏览(134)

如果我的设备连续多次无法推送通知,我希望将其视为已卸载,并从fcmtoken表中删除它。如果无法发出通知,我如何从FCM服务器接收响应?
代码:OnMessageReceived

public override void OnMessageReceived(RemoteMessage message)
{
    try
    {
        Log.Debug(TAG, "From: " + message.From);
        Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);

        // Create an intent to open the MainActivity when the notification is clicked
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

        // Build the notification
        if (message.GetNotification() != null)
        {
            string title = message.GetNotification().Title;
            string body = message.GetNotification().Body;

            var notificationBuilder = new NotificationCompat.Builder(this, PRIMARY_CHANNEL)
                    .SetSmallIcon(Resource.Drawable.splash_logo)
                    .SetContentTitle(title)
                    .SetContentText(body)
                    .SetAutoCancel(true)
                    .SetPriority(NotificationCompat.PriorityHigh)
                    .SetContentIntent(pendingIntent);

            var notificationManager = NotificationManagerCompat.From(this);
            notificationManager.Notify(notificationId++, notificationBuilder.Build());   
        }
    }
    catch (System.Exception ex)
    {
        Log.Error(TAG, ex.Message);
    }
}
332nm8kg

332nm8kg1#

我不知道如何为客户端做到这一点,但这个方法对我的服务器工作。

using FirebaseAdmin.Messaging;
using System.Collections.Generic;
using System.Threading.Tasks;
using xamarinchatsr.Web.Models;

namespace xamarinchatsr.Web.Services
{
    public class FMCNotificationService : IFMCNotificationService
    {
        private readonly IDBService _DB;
        public FMCNotificationService(IDBService db)
        {
            _DB = db;
        }
        /// <summary>
        /// The method sends a message to FCM
        /// </summary>
        public virtual async Task<string> SendNotification(List<string> registrationTokens, FMCNotification fMCNotification)
        {
            var message = new MulticastMessage()
            {
                Tokens = registrationTokens,
                Data = new Dictionary<string, string>()
                {
                    {"groupid", fMCNotification.GroupId.ToString()},
                },
                Notification = new Notification()
                {
                    Title = fMCNotification.GroupName,
                    Body = fMCNotification.SenderName + ": " + fMCNotification.Message
                }
            };

            var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(message).ConfigureAwait(true);
            // If there are errors when sending the notification
            if (response.FailureCount > 0)
            {
                var failedTokens = new List<string>();
                for (var i = 0; i < response.Responses.Count; i++)
                {
                    if (!response.Responses[i].IsSuccess)
                    {
                        // The order of responses corresponds to the order of the registration tokens.
                        failedTokens.Add(registrationTokens[i]);
                    }
                }
                // Delete invalid tokens
                _DB.DeleteTokens(failedTokens);
            }
            return "";
        }
    }
}

相关问题