Silent push notification is not working - ios

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.

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.

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);
}

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.

Related

APNs: Change the app badge while the app is the foreground

I have an app and a server-side push sender. When new notifications arrive, the server sends an empty push message which only contains a badge update.
When the app is in the background, the badge is successfully updated. However, when the app is in the foreground, the badge is not updated at all - the push is delivered to the app, which discards it.
The obvious workaround is to catch the push and update the badge from within the app. For some technical reasons this would take some time to take effect (development time, app store check time, users who don't frequently upgrade etc.)
I wonder if there's a way to circumnavigate this and update the badge using a server side APNs push regardless of the app state, foreground or background.
Is there a way to change an iOS app badge using a push message, when the app is in the foreground, without handling the push notification from within the app?
This can only be achieved through application delegate methods defined in your AppDelegate
Deprecated in iOS 10
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
or,
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
The above delegate functions gets called when app is in foreground there you can decode your Push Payload and assign the application badge as follows
[UIApplication sharedApplication].applicationIconBadgeNumber=[[userInfo objectForKey:#"aps"] valueForKey:#"badge"];
Cheers.

Launch an app with push notification after it has been terminated

I was wondering if there was a way to wake up an app that has been terminated by the user on ios8-9. By terminated I mean double click on the home button and swipe up.
Is it somehow possible to launch an app by sending a silent push notification so that didreceiveremotenotification gets fired and gives me some runtime ?
I have noticed that a fair share of my users terminate my app. As I rely heavily on background fetch, this a problem. My idea was to send silent push notifications to launch the app in the background and trigger background fetch.
Short Answer: No That is not possible.
Detail:
When there is any new content on server you will send Remote Notification to your application to inform about that. (A Remote Notification is really just a normal Push Notification with the content-available flag set)
When application received this Remote Notification it calls following method:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
In Documentation of this method it is clearly written:
However, the system does not automatically launch your app if the user
has force-quit it. In that situation, the user must relaunch your app
or restart the device before the system attempts to launch your app
automatically again.
Reference:
objc.io: Remote Notifications
Apple Doc about application:didReceiveRemoteNotification:fetchCompletionHandler:

Launch app in background automatically?

Is there a way to persist an iOS app in the background such that it starts up automatically when the device is turned on and will re-launch later if terminated? I have read a few posts on either voip and gps services. My app sends emergency SMS messages with the user's location. It needs to remain active to listen for distress triggers. Would it be possibly to start up the app automatically with sending push notifications to the app using an APNS server?
Yes, you can start your app using APNS.
1) You have to set "content-available" to 1 in the notification body.
2) You have to implement:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
3) And have the right background modes: Background Modes
But be careful, your users can turn this off by disabling Background App Refresh in the iPhone Settings. You can check this in code:
[UIApplication sharedApplication].backgroundRefreshStatus!=UIBackgroundRefreshStatusAvailable

How to consume iOS Push notification in the app only

I am working on a app, where I need to send a push notification to the app to start processing data as needed.
How do I send a push notification to the device so that instead of showing the alert message, the notification is forwarded to the app - whether the app is in the foreground or background..
I did implement the delegate method :
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
however this is called only when the app is in the foreground. When the app is in the background the notification shows up on the notification center.
Please advice.
To make this works you need to do few step:
set background mode remote-notification
implement application:didReceiveRemoteNotification:fetchCompletionHandler: method in app delegate
and make sure push notification payload contains key "content-available" : 1
related docs:
App States and Multitasking
Local and Push Notifications in Depth
Is your application using a backend? An example would be Parse or another to store your data.
Parse, for example, allows you to add push notifications that can be customized and changed to get the most out of your application.

iOS; Push notification is not work when the app is not running or in background

I have a problem with detecting a push notification from APNS.
If there is a push notification from APNS when the app is not running or in background,
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
is not work.
And when I'm back to the foreground without selecting a push notification message from notification centre, it is not work.
How can I solve this problem?
I'm testing the app in iOS 6.13 and iOS 7.0.4.
Thanks for your help. :)
Actually,the first receiver of notifications is system,not your app.
If your app isn't in foreground, app's application:didReceiveRemoteNotification:fetchCompletionHandler will never be called until you tap the notification to make your app becomes to the foreground again.
When a push notification arrives and the user clicks 'cancel', your app has no way to read that push notification again. You have to implement a separate functionality (most probably on server-side) to fetch a list of notifications sent to this device.
From Apple documentation:
Implement this method if your app supports the remote-notification background mode.
Which mean the method you are using focuses on the background task. And only available after iOS 7.0.
I recommend you use application:didReceiveRemoteNotification:.
From Apple documentation:
If the app is running and receives a remote notification, the app calls this method to process the notification.

Resources