1

I'm using silent notifications to wake up my app from background. When I close my app everything works fine, but when I terminated it(swipe up) it stops working. I can feel(vibration) that silent notification has been delivered but my app doesn't proces it. I made an local notification that should fire when silent notifications has arrived. In my .plist I have Remote notifications and Background fetch enabled. In AppDelegate.m I have this:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

{

    UILocalNotification *notification_ = [[UILocalNotification alloc]init];

    notification_.alertBody = [NSString stringWithFormat:@"Psst!"];

    notification_.soundName = UILocalNotificationDefaultSoundName;

    [notification_ setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];

    [notification_ setTimeZone:[NSTimeZone defaultTimeZone]];

    [application setScheduledLocalNotifications:[NSArray arrayWithObject:notification_]];

}
user894132
  • 33
  • 1
  • 6

1 Answers1

1

The application:didReceiveRemoteNotification is only executed when you're app is actually running be it in the foreground and background. That is why the aforementioned method won't be called after you terminate the app.

When your app is not running and it's opened from notification, the notification should be handled in application:didFinishLaunchingWithOptions instead. The launchOptions dictionary should contain key UIApplicationLaunchOptionsRemoteNotificationKey which refers to the notification payload.

For more information, check out the "Handling Local and Remote Notifications" section of Local and Remote Notification Programming Guide

Markus
  • 979
  • 8
  • 10
  • I need to deliver silent push to run my app once a day in background, and now I'm a little bit confused. To accomplish this while app is terminated I need to use `UIApplicationLaunchOptionsRemoteNotificationKey` in `application:didFinishLaunchingWithOptions`? – user894132 Feb 15 '15 at 18:28
  • Sorry, I didn't notice the silent part there. Even though, AFAIK silent notification is just a notification that has `content-available` as part of its payload. Handling it in `application:didFinishLaunchingWithOptions` doesn't really make sense that much. – Markus Feb 16 '15 at 07:02
  • How can I handle silent push when app is terminated? – user894132 Feb 16 '15 at 15:30
  • Apparently, you can't handle silent notifications when the app is terminated. See [this answer](http://stackoverflow.com/a/19202487/499798) for more in-depth explanation. – Markus Feb 16 '15 at 15:34
  • So there is no way to wake up my app in background? – user894132 Feb 16 '15 at 15:44