Huu Bao Nguyen
Huu Bao Nguyen

Reputation: 1181

How to don't show Firebase notification when the application in Foreground on Xamarin iOS?

I'm developing a chat application, I use Firebase for push notification.

In Background and Foreground, Method DidReceiveRemoteNotification() work well.

But when the application in Foreground, I don't want to show Firebase notification because It annoys the user. I just want to handle the event when the application receives Firebase notification and don't show Firebase notification.

Firebase_push_notification

I tried removing 2 params alert-title and alert-body on config of Firebase:

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

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

After changing Firebase config, I can't push Firebase notification when the application turns off.

So, I must use First config.

=> How to don't show Firebase notification when the application in Foreground on Xamarin iOS?

This is my code:

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);
        }
    }

Please help me!

Upvotes: 0

Views: 1359

Answers (2)

APS
APS

Reputation: 21

One possible way to show / hide notification when application is in foreground is to set UNNotificationPresentationOptions in AppDelegate FinishLaunching method.

By default when app is in foreground, UNNotificationPresentationOptions is set to None, causing notification to not show up when application is not in foreground. But for your case it seems that it is set to value other then None.

UNNotificationPresentationOptions is defined as

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.
}

//To set for alert

FirebasePushNotificationManager.CurrentNotificationPresentationOption = UNNotificationPresentationOptions.Alert;

//You can also combine them

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

Reference: https://github.com/CrossGeeks/FirebasePushNotificationPlugin/issues/6

Upvotes: 1

Junior Jiang
Junior Jiang

Reputation: 12723

From FCM document about receiving message ,

In iOS 10 and above, you can set the UNUserNotificationCenter delegate to receive display notifications from Apple and FIRMessaging's delegate property to receive data messages from FCM. If you do not set these two delegates with AppDelegate, method swizzling for message handling is disabled. You'll need to call appDidReceiveMessage: to track message delivery and analytics.

// 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);
}

So you can change what to show (Or no alert) when meeage receive while app is in the foreground.If completionHandler set UNNotificationPresentationOptionNone then there will no alert. You can have a try with this way.

UNNotificationPresentationOptionNone : No alert.

Upvotes: 0

Related Questions