Push notifications not working while app is in background

Viewed 85

In my Xamarin.iOS app, I have push notifications working so I get a notification/banner that I can click on and it opens a UIViewController. In my FinishedLaunching method in my AppDelegate:

if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
    // For iOS 10 display notification

    var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
    UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
    {
        Console.WriteLine(granted);
    });
    UNUserNotificationCenter.Current.Delegate = this;
}
else
{
    // iOS 9 or before
    var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
    var FCMsettings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
    UIApplication.SharedApplication.RegisterUserNotificationSettings(FCMsettings);
}

UIApplication.SharedApplication.RegisterForRemoteNotifications();

Messaging.SharedInstance.Delegate = this;

I then implement WillPresentNotification and DidReceiveMessage But they only get called when my app is in the foreground. If it's in the background, I will only see the banner when I open the app again. I want to see the banner while the app is in the background, and then when I tap on it, it opens my app. How can I do this?

This is my code that creates the notification/banner:

UNMutableNotificationContent notification = new UNMutableNotificationContent();
notification.Title = title;
notification.Body = msg;
notification.CategoryIdentifier = channel;
var requestID = channel;
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(0.1, false);
var request = UNNotificationRequest.FromIdentifier(requestID, notification, trigger);
              UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
              {
                if (err != null)
                {
                    // TODO
                }
              });

It gets called from DidReceiveMessage in my AppDelegate, and appears at the top of the screen for a few seconds, but if my app is in the background, DidReceiveMessage will not get called until my app is brought to the foreground again. Only then will I see it.

My AppDelegate implements UserNotifications.IUNUserNotificationCenterDelegate and Firebase.CloudMessaging.IMessagingDelegate. Here are some of the functions it implements:

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

[Export("messaging:didReceiveMessage:")]
public async void DidReceiveMessage(Messaging messaging, RemoteMessage remoteMessage)
{
    // Look at remoteMessage.AppData key/value pairs and present notification if needed
}

[Export("messaging:didReceiveRemoteNotification:fetchCompletionHandler:")]
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
    // check userInfo key/value pair and display notification if needed
}
1 Answers
Related