Xamarin Forms IOS - Silent Remote Notifications doesn't run when app is in background

dai15002dai15002 Member ✭✭

Hello. I have implemented the silent remote notification for IOS (link: https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/updating-an-application-in-the-background#remote-notifications-ios-7-and-greater). When a user gets a notification containing a message text then the text should appear in notification and a refresh should be done in the app. I am able to show the message but no refresh is done when the app is in the background, until the user clicks the notification and the app is started on foreground.

As the notification payload from the server I do send "content-available" : 1 into the app, and in the app I receive it in AppDelegate as below:

[Export("application:didReceiveRemoteNotification:fetchCompletionHandler")]
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action completionHandler)
{
NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
string messageText = (aps[new NSString("message")] as NSString).ToString();

//show the notification
UIAlertView avAlert = new UIAlertView("Title", messageText, null, "Ok", null);
avAlert.Show();

//do some refreshing 
//...

}

The registeration for the notification is as below:

private void registeredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
const string templateBodyAPNS = "{\"aps\":{\"alert\":\"$(message)\",\"message\":\"$(message)\",\"content-available\":\"$(content-available)\"}}";

//register the push
//...

}

Sign In or Register to comment.