2
votes

I am trying to achieve silent push notification.I need to save the silent push notification data in my database in app so that I can show the unseen notification and its count to users when the users uses the app. I am using Xcode 6.1 and targeting iOS 7 and later devices. Currently, I am using development certificates for push notification. I have checked remote notification in the background modes of target project capabilities, also the info-list's background mode has "App downloads content in response to push notifications" in required background modes.

In my AppDelegate.m, I am able to register remote notification and also get the device token. Now when I send push notification, my delegate method gets called when app is in foreground but when app is in background/not running, it doesn't get called though I receive the push notification in banner.

I have implemented this method in AppDelegate.m

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    NSLog(@"Received push notification");
}

The payload I am sending looks like this:

{ 
  aps: { 
          content-available: 1 
       }
}

I tried adding priority, sound etc. But nothing helped me. Can someone help me to figure out how to save silent push notification data in app when app is in running in the background or not running? Kindly help me to resolve this issue.

3

3 Answers

0
votes

So, you should definitely be able to receive them in the background, but be warned that if the user deliberately kills your app from the task switcher then it won't get them anymore.

With iOS 7 and above the callback is as you say, but before that it was

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

Seems unlikely you are targeting iOS 6 though so I would go with either a malformed push packet or background push settings not being quite right.

0
votes

Do you have the apps info.plistUIBackgroundModes set to remote-notificiton?

Then also add this method to help you debug

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
   //Success
   handler(UIBackgroundFetchResultNewData);
}
0
votes

Silent push notifications are different than user-facing notifications. They are treated as low priority and are rate limited by both APNS and iOS. In practice, this means that silent notifications can only be sent infrequently and there may be a very long delay before they are delivered to an application.

The behavior you are describing is likely the wakeup rate limiter on the device. iOS limits how often apps are launched to perform background work. This is to prevent abusive behavior - some apps might want to stay alive in the background draining the battery forever.

The wakeup rate limiter is not active when Xcode is attached, which makes silent notifications appear to be delivered instantly.

My iOS Notifications book has a lengthy chapter describing the rate limiters and how to work with them.