locked
ReceivedRemoteNotification not called, when app is closed RRS feed

  • Question

  • User68624 posted

    I have an app that is receiving notifications from the iCloud server. In the app's first run, the device is subscribed to receive notifications whenever a record is updated, created or deleted. The subscriptions work perfectly, and I've checked that but only if the app is opened.

    Jumping into details: I have configured the app to allow the Background Refresh & remote-notification in plist Also I have registered the device to receive remote notifications, using the function RegisterForRemoteNotificationTypes() in the AppDelegate.cs

    The functions ReceivedRemoteNotification() & DidReceiveRemoteNotification() work perfectly and called, but only when the app is opened. But when the app is closed nothing is fired.

    Appreciate your help, or pointing me to the direction to fix this

    Tuesday, April 21, 2015 10:32 AM

Answers

  • User50971 posted

    How are you closing the app?

    Documentation says:

    Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.


    So I believe you're force-quit the app. Try to restart the device so no app is opened, and send the notification.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, April 21, 2015 11:45 AM
  • User50971 posted

    No problem. Yeah, as stated on the method description if the app is force-quitted it will not work unless the user opens the app or a restart happens. I guess it kinda makes sense.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, April 21, 2015 12:44 PM
  • User50971 posted

    hi @abe.123 that's very useful information, thanks for sharing!

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, May 22, 2015 2:13 PM

All replies

  • User50971 posted

    closed as in running on the background? or closed, closed?

    Tuesday, April 21, 2015 11:29 AM
  • User68624 posted

    @Nuninz , I meant closed completely as closed from the multitasking.

    Tuesday, April 21, 2015 11:41 AM
  • User50971 posted

    How are you closing the app?

    Documentation says:

    Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.


    So I believe you're force-quit the app. Try to restart the device so no app is opened, and send the notification.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, April 21, 2015 11:45 AM
  • User68624 posted

    @Nuninz , yes I've force-quitted my app. My intention was to receive the remote notification regardless the state of the app.

    Basically, the notifications are received from iCloud, the notifications aim to notify the app itself for a change happened to the data on iCloud (add new record, modify or delete an existing record). When the app receives this notification, the app fetches that record and update the local storage. That's it, no notification will be presented to the user, and basically the user don't even have to know that the app received a remote notification.

    Another way to describe the logic of this process, the app's logic in this process similar to the "Notes" app in any iDevice, whenever you've added a new note on a iDevice, the notes are being pushed from iCloud to all the other registered devices. Regardless if the "Notes" app is on the background, foreground, or even if the app is not opened at all.

    As I've mentioned, this happens perfectly, whenever the app is on the foreground or the background. But whenever I close "force-quit" the app, nothing happens.

    I don't know if this logic makes sense in the current implementation or the mechanism is faulted.

    Once again I really appreciate your fast feedback and responses, and wishing for guidance. Thanks in advance.

    Tuesday, April 21, 2015 12:26 PM
  • User50971 posted

    No problem. Yeah, as stated on the method description if the app is force-quitted it will not work unless the user opens the app or a restart happens. I guess it kinda makes sense.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, April 21, 2015 12:44 PM
  • User3151 posted

    @Nuninz is correct, force-quitting the app will cause notifications to not reach the app until the user taps on them from the Notifications screen.

    Tuesday, April 21, 2015 1:08 PM
  • User79712 posted

    I'm also having this issue.. So is there a way to call such a method that processes data from a remote notification while the app is not running at all?

    Friday, May 22, 2015 1:08 PM
  • User68624 posted

    @TassoNichas, as stated by both @DannyC and @Nuninz (Thanks a lot for them) both the functions ReceivedRemoteNotifications and DidReceivedRemoteNotifications can only be fired if the app is running either in the background or in the foreground.

    There would be no other way to get or process the notifications, if the app is not running at all, or force-quitted.

    However, if your app is registering the device for push notifications, when the app is opened after it was force-quitted, those functions will be called and then the request will be processed.

    The problem/catch here, is Apple Push Notification Server (APNS), will only store the last notification to be pushed not all of the notifications, ONLY THE LAST ONE.

    If you're dealing with an iCloud/CloudKit database that you're syncing the data or something similar to that (my exact case): I've found a nice solution, the function CKFetchRecordChangesOperation(CKServerChangeToken) will retrieve the latest record(s) that was added, updated or deleted. You will need to pass the CKServerChangeToken if you have one (it will retrieve the received notifications since that token), or just call the function CKFetchRecordChangesOperation() with no parameters and that's it, it will retrieve all the notifications received since forever.

    The notifications received, could be the modifications happened to the CloudKit database or any other notifications shuld have been received by the device.

    The return value of this function, is containing the records changed CKNotification (which will contain the NotificationType, RecordID, QueryNotificationReason...etc.) and the new server token CKServerChangeToken, that you can use for your next call.

    Here's a link for the function on Xamarin API website: http://iosapi.xamarin.com/?link=T%3aCloudKit.CKFetchNotificationChangesOperation%2f* Link on for the function on Apple Developer website: https://developer.apple.com/library/prerelease/ios/documentation/CloudKit/Reference/CKFetchNotificationChangesOperation_class/index.html

    Hope that would help you.

    Friday, May 22, 2015 1:58 PM
  • User50971 posted

    hi @abe.123 that's very useful information, thanks for sharing!

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, May 22, 2015 2:13 PM
  • User79712 posted

    @abe.123 Yes very helpful indeed, thanks for the post!

    Monday, May 25, 2015 2:00 PM