Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS retrieve remote notification payload

I have implemented the UIApplicationDelegate method -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler to handle push notifications, it works fine. In some situations, I need to retrieve the payload of the push notification. But sometimes this delegation method is not called.

I have a question about retrieving the payload (userInfo) in this scenario:

The app running either in background or not launched. The app received a push notification, a banner is shown, sound is played, and message is displayed, app icon badge increased, and the push notification can be seen in the iOS notification centers. If the user taps the notification when it's shown or in the notification center, the app launches, and the didReceiveRemoteNotification: method is called.

However, if the user just tap the app icon and launch the app in normal ways, the method is not called, and I can't retrieve the userInfo.

From Apple's documentation on handling push notifications:

The notification is delivered when the app isn’t running in the foreground. In this case, the system presents the notification, displaying an alert, badging an icon, perhaps playing a sound, and perhaps displaying one or more action buttons for the user to tap.

The user taps a custom action button in an iOS 8 notification. In this case, iOS calls either application:handleActionWithIdentifier:forRemoteNotification:completionHandler: or application:handleActionWithIdentifier:forLocalNotification:completionHandler:. In both methods, you get the identifier of the action so that you can determine which button the user tapped. You also get either the remote or local notification object, so that you can retrieve any information you need to handle the action.

The user taps the default button in the alert or taps (or clicks) the app icon. If the default action button is tapped (on a device running iOS), the system launches the app and the app calls its delegate’s application:didFinishLaunchingWithOptions: method, passing in the notification payload (for remote notifications) or the local-notification object (for local notifications). Although application:didFinishLaunchingWithOptions: isn’t the best place to handle the notification, getting the payload at this point gives you the opportunity to start the update process before your handler method is called.

If the notification is remote, the system also calls application:didReceiveRemoteNotification:fetchCompletionHandler:.

If the app icon is clicked on a computer running OS X, the app calls the delegate’s applicationDidFinishLaunching: method in which the delegate can obtain the remote-notification payload. If the app icon is tapped on a device running iOS, the app calls the same method, but furnishes no information about the notification.

The highlighted part kinda saying there is no obvious way to access the payload of the push notification in this way. So is there a way to work around this issue?

Thank you!

like image 540
Jing Avatar asked Apr 19 '15 23:04

Jing


2 Answers

It is the default behavior of iOS. You will never know the payload unless user opens your app via tapping on the notification.

like image 158
atulkhatri Avatar answered Nov 15 '22 12:11

atulkhatri


You can use silent push notifications if you want to send custom data (max 2048 bytes) or commands to your mobile app. In the push notification set 'content-available' to 1 so you app gets some time to process data and also add your custom content to the notification.

I also use silent push notifications to trigger my apps to update by REST HTTP calls, since my data can be bigger than the max size... The only downside is that Apple might block your background activities if you are battery or processor intensive. Visual push notifications are always delivered.

Things I hate most are visual push notifications like 'you've got a new message' , on which I click sometimes when no data connection is available and the app will not show me the message... You can solve this by sending a silent push notification with data and only if the data could be fetched from the server, or stored correctly in case you can send it all in the notification, you set a local notification.

Choose between visual and silent push notifications wisely is my only advise.

Check the following link for more official Apple info: https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html

like image 34
WiRa Avatar answered Nov 15 '22 12:11

WiRa