简体   繁体   中英

DidReceiveRemoteNotification is not called in background - Xamarin iOS - Push Notifications

So I'm developing an app in Xamarin iOS and it requires push notifications be sent to update the offline sync'd data sporadically. However it seems that the DidReceiveRemoteNotification function only runs when the app is in the foreground!

I understand that it won't work if the app is closed but it should work while in the background right? The documentation seems to support this too. Through my own research, I can see that Native apple code has two different DidReceiveRemoteNotification's functions (didReceiveRemoteNotification and didReceiveRemoteNotification:fetchCompletionHandler) and the latter is used for handling background pushes. I'm really stuck as everything seems to suggest it should call it but it just doesn't.

Here's my function:

public override async void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> 
 completionHandler)

{

        Debug.WriteLine($"Notifcation Received - {userInfo.ToString()}");

        //Call to an empty Azure function to prove it is being called!
        //This allows me to check whether or not this code is eecuted without breakpoints in VS

         using (HttpClient client = new HttpClient()) 
            await client.GetAsync(FUNCTION_URL);


        //.. My code to pull latest data here

        completionHandler(UIBackgroundFetchResult.NewData);
}

And all I am sending it is a simple: "{\\"aps\\": {\\"content-available\\": 1 }}";

Background mode is set in the info.plist too

Can anyone point me in the right direction? Thanks!

So I found the answer myself. As it turns out IOS 11 is pretty bad for silent push notifications!

I was using iOS 11.1.x versions and it just wasn't working at all, I upgraded my phone to iOS 11.2.5 and it works perfectly now.

The DidReceiveRemoteNotification function is being called as expected and the data is being retrieved in the background!

If anyone else is having this issue with either native or xamarin apps, check your iOS version!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM