【问题标题】:How to don't show Firebase notification when the application in Foreground on Xamarin iOS?如何在 Xamarin iOS 上的前台应用程序时不显示 Firebase 通知?
【发布时间】:2020-01-27 00:10:03
【问题描述】:

我正在开发一个聊天应用程序,我使用Firebase 进行推送通知。

BackgroundForeground 中,方法DidReceiveRemoteNotification() 运行良好。

但是当应用程序在Foreground 中时,我不想显示Firebase notification,因为它会惹恼用户。我只想在应用程序收到Firebase notification 并且不显示Firebase notification 时处理该事件。

我尝试在 Firebase 的配置上删除 2 个参数 alert-titlealert-body

第一http://{url}/demo?device-token={token}&alert-title={title}&alert-body={body}

稍后http://{url}/demo?device-token={token}

更改Firebase 配置后,应用程序关闭时我无法推送Firebase notification

所以,我必须使用First 配置。

=> Foreground 中的应用程序在Xamarin iOS 上时如何不显示Firebase notification

这是我的代码:

public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
        try
        {
            // App in Foreground
            if (!_isInBackground)
            {
                // Foreground
                if (userInfo?.ContainsKey(new NSString("payload")) == true)
                {
                    // TODO: handle Foreground
                    return;
                }
            }

            // App in Background
            // Checking push notification message
            if (userInfo?.ContainsKey(new NSString("payload")) == true)
            {
                var payload = userInfo[new NSString("payload")]?.ToString();

                if (!string.IsNullOrEmpty(payload))
                {
                    // TODO: handle Background

                }

                // Push notification message
                PushNotificationManager.DidReceiveMessage(userInfo);

                // Inform system of fetch results
                completionHandler(UIBackgroundFetchResult.NewData);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

请帮帮我!

【问题讨论】:

  • 您好,能否在 Xamrain.ios 中显示AppDelegate.cs 的代码,这可以从那里实现。

标签: xamarin xamarin.forms xamarin.ios


【解决方案1】:

当应用程序处于前台时显示/隐藏通知的一种可能方法是在 AppDelegate FinishLaunching 方法中设置 UNNotificationPresentationOptions。

默认情况下,当应用程序处于前台时,UNNotificationPresentationOptions 设置为 None,导致应用程序不在前台时不会显示通知。但是对于您的情况,它似乎设置为 None 以外的值。

UNNotificationPresentationOptions 定义为

public enum UNNotificationPresentationOptions
{
    Alert,  //Display the notification as an alert, using the notification text.
    Badge,  //Display the notification badge value in the application's badge.
    None,   //No options are set.
    Sound  //Play the notification sound.
}

//设置为警报

FirebasePushNotificationManager.CurrentNotificationPresentationOption = UNNotificationPresentationOptions.Alert;

//也可以组合起来

FirebasePushNotificationManager.CurrentNotificationPresentationOption = UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Badge;

参考:https://github.com/CrossGeeks/FirebasePushNotificationPlugin/issues/6

【讨论】:

    【解决方案2】:

    来自 FCM document about receiving message

    在 iOS 10 及更高版本中,您可以设置 UNUserNotificationCenter 委托以接收来自 Apple 的显示通知,并设置 FIRMessaging 的委托属性以接收来自 FCM 的数据消息。如果您不使用 AppDelegate 设置这两个委托,则禁用消息处理的方法调配。您需要致电 appDidReceiveMessage: 来跟踪消息传递和分析。

    // Receive displayed notifications for iOS 10 devices.
    // Handle incoming notification messages while app is in the foreground.
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center
           willPresentNotification:(UNNotification *)notification
             withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
      NSDictionary *userInfo = notification.request.content.userInfo;
    
      // With swizzling disabled you must let Messaging know about the message, for Analytics
      // [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
    
      // Print message ID.
      if (userInfo[kGCMMessageIDKey]) {
        NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
      }
    
      // Print full message.
      NSLog(@"%@", userInfo);
    
      // Change this to your preferred presentation option
      completionHandler(UNNotificationPresentationOptionNone);
    }
    

    因此,当应用程序处于前台时,您可以更改当 meeage 接收时显示的内容(或没有警报)。如果 completionHandler 设置 UNNotificationPresentationOptionNone 则不会有警报。你可以试试这个方法。

    UNNotificationPresentationOptionNone:没有警报。

    【讨论】:

    • 但是传递给参数的方法DidReceiveRemoteNotification()Action&lt;UIBackgroundFetchResult&gt; completionHandler。所以,我不能设置completionHandler(UNNotificationPresentationOptionNone);
    • @HuuBaoNguyen Okey,你能把AppDelegate.cs的文件给我看看吗,我去看看。
    • 方法DidReceiveRemoteNotification()包含在AppDelegate.cs文件中。
    • @HuuBaoNguyen 然后显示DidReceiveRemoteNotification的内容,我去看看。
    • 我更新了关于我的问题的代码...请检查一下!谢谢!
    猜你喜欢
    • 2018-11-08
    • 1970-01-01
    • 2021-04-08
    • 2017-11-10
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 2018-06-15
    • 2020-06-18
    相关资源
    最近更新 更多