首页 > 解决方案 > 条件处理 Firebase 推送通知

问题描述

我有一个支持 Android 和 iPhone 的 Xamarin.Forms 应用程序。该应用使用 Firebase for Android 和 Azure Notification Hub for iOS 发送和接收推送通知。如果应用程序从发件人的手机发送通知,组中的多个人会收到并可以处理它。但这不是我们需要的。我只希望第一个接收者处理通知,其他人应该忽略它。有没有好的方法来实现它?

这是我在 Android 上处理推送通知的代码:

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FirebaseService : FirebaseMessagingService
{
    public override void OnMessageReceived(RemoteMessage message)
    {
        try
        {
            base.OnMessageReceived(message);

            string messageBody = message.GetNotification()?.Body;

            VideoCallMessage videoCallMessage = JsonConvert.DeserializeObject<VideoCallMessage>(messageBody);

            if (App.UserContext.IsEmployee)
            {
                // convert the incoming message to a local notification
                SendLocalNotification(messageBody);
            }
        }
        catch (Exception ex)
        {
            Log.Debug("FirebaseService.OnMessageReceived()", $"Exception in OnMessageReceived(). ErrorMessage: {ex.Message}, Stack Trace: {ex.StackTrace}");

            Microsoft.AppCenter.Crashes.Crashes.TrackError(ex); // Report the exception to App Center
        }
    }

对于 Delegate.cs 中的 iOS:

    [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
    public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
    {
        completionHandler();

        NSDictionary userInfo = response.Notification.Request.Content.UserInfo;
        ProcessNotification(userInfo);
    }

    [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
    public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);

        NSDictionary userInfo = notification.Request.Content.UserInfo;
        ProcessNotification(userInfo);
    }

标签: c#xamarin.formspush-notificationfirebase-cloud-messaging

解决方案


推荐阅读