4

I’m now using didReceiveRemoteNotification to get the payload of the notification pushed from Parse, however, it is only triggered when the notification is tapped and opened.

What I’m trying to do is start an alarm to remind the user that a notification has arrived, so I guess didReceiveRemoteNotification is not what I’m looking for. Which method should I look into for this purpose?

Thank you!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Alison
  • 431
  • 6
  • 19
  • Kindly read the below answer which is a working model for downloading data even if the app is not running, but provided you have enabled background modes for `fetch`. – Balram Tiwari May 18 '15 at 16:08

4 Answers4

3

Words from The WWDC 2014 Whats New in iOS Notifications

Local and push notifications let background or inactive apps notify users that an event of interest has occurred, or that an app has new information for them.

The WWDC 2013 Whats New With Multitasking tells us how to get this work.

• add UIBackgroundModes : remote-notification in info.plist

enter image description here

• add `content-available: 1 in your Payload while sending from server

enter image description here

• lets iOS handle it to open your app for background mode

enter image description here

For iOS 10 and above you have to switch ON the Background Modes from your Target -> Under Capabilities and check mark the required fields.

enter image description here

Now you can set your alarm as you want. you may set a scheduled local notification until user interact with app

Community
  • 1
  • 1
M Zubair Shamshad
  • 2,741
  • 3
  • 23
  • 45
  • Hi Zubair, I've tried your solution and it works when the app is running in the background, thank you for the help! However, when I force quit / swipe up to kill the app, it cannot retrieve the payload content, seems that "application: didReceiveRemoteNotification:fetchCompletionHandler:" is not called in this case? Am I missing anything here? Thanks again! – Alison Jun 01 '15 at 07:38
  • @qwerty There is nothing to worry about the code. You have to it as per your requirement after receiving notification. All you need to update your `plist` and notification `payload` . Then you can retrieve your notification in `didReceiveRemoteNotification:fetchCompletionHandler` – M Zubair Shamshad Aug 18 '16 at 04:04
  • This method is not triggered in iOS 10 and above. Also which method is triggered when a remote notification is received without user pressing on the payload? – iPhoneDeveloper Aug 31 '18 at 12:47
  • Thats so cool. This is exactly what i wanted. Thanks a ton Zubair. – iPhoneDeveloper Sep 03 '18 at 12:28
  • Upvote given. I have one more scenario suppose the user has killed the app and he receives a remote notification, how do you get notified about received notification? Your previous answer works when the app is running and is in the background, but how to get notified when the user has killed the app? – iPhoneDeveloper Sep 03 '18 at 12:41
1

You can do nothing with the push notification unless the user taps on the notification banner OR the app is in foreground. In background, you do not have control. Reference Apple Push Notification setting up Remote Notifications method overrides other methods

Community
  • 1
  • 1
Fawad Masud
  • 12,219
  • 3
  • 25
  • 34
1

Below method is invoked when a remote notification is received in iOS.

func application(_ application: UIApplication,
                 didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // do something
}

Apart from basic setup for remote notifications you should set below flag in payload of notification.

"content-available": 1
Pranav Gupta
  • 741
  • 9
  • 10
0

You must look for this method in app delegate:-

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Now check whether your app has received any push notification/'s or not

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (notification) {
        NSLog(@"app received a notification %@",notification);
        [self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
    }else{
        NSLog(@"app did not receive a notification");
    }
Vizllx
  • 9,135
  • 1
  • 41
  • 79