-2

How can I call the method didReceiveRemoteNotification in didfinishlaunchingwithoptions? I'm using swift language. when the user directly opens the app, the notification cancelled and the operations performed in didReceiveRemoteNotification not called. How to handle this ?

I've reached upto here. But unable to complete

if let options = launchOptions, notification = options[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {
self.application(application, didReceiveRemoteNotification: notification, fetchCompletionHandler: _______)

        }
Rosemol J
  • 183
  • 4
  • 19
  • @SausageMachine In didReceiveRemoteNotification I am saving this notification data. But when a notification came, app opened (without clicking the notification) , the notification automatically cancelled and save action is not performing – Rosemol J Aug 18 '16 at 04:25
  • Its doesn't matter which language are you using Swift / Objective c but process will se same for Push notification. DidReceiveRemoteNotification method will call when push notification will receive and the tap on notification. – Anand Nimje Aug 18 '16 at 04:29
  • But when the user directly opens the app, the notification cancelled and the operations performed in didReceiveRemoteNotification not called. How to handle this ? – Rosemol J Aug 18 '16 at 04:32
  • you can use local notifications. – Anand Nimje Aug 18 '16 at 04:34
  • @Anand Notifications are send from server – Rosemol J Aug 18 '16 at 04:36

1 Answers1

4

As per docs,

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.

For remote notifications, the system also calls the application:didReceiveRemoteNotification:fetchCompletionHandler: method of the app delegate.

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 notification is delivered when the app is running in the foreground. The app calls the application:didReceiveRemoteNotification:fetchCompletionHandler: or application:didReceiveLocalNotification: method of the app delegate. (If application:didReceiveRemoteNotification:fetchCompletionHandler: isn’t implemented, the system calls application:didReceiveRemoteNotification:.) In OS X, the system calls application:didReceiveRemoteNotification:.

You can find the link for the same in here : https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1

Summary : You have to handle APNS in 3 situations :

  1. App is in Foreground/App is in background (but not suspended) : application:didReceiveRemoteNotification: method of the app delegate is called and payload is handed over to you.

2.App is suspended/killed and user taps on notification or Alert: application:didFinishLaunchingWithOptions: of app delegate gets called and you can access payload from launchOption.

3.App is suspended/killed and user taps on App Icon : You will not receive any info about APNS. Simplest solution you can do is once the app becomes active make a web service call to web server fetch all the updated info and update your UI and application badge count :)

Hope it helps :)

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78