iOS 7 - silent push notifications - ios

I have setup silent push notification and all works fine when app is in foreground or in background.
The problem is when the application is not active/killed (if i have understand well, any application is killed automatically after 30 seconds when it is in background).
My payload is like this
{"aps":{"alert":"test","sound":"bingbong.aiff","badge":33,"content-available":1}}
All works fine but when i receive this push, badge icon is not update (no 33 is appear near the application icon). This is the first problem.
The second problem is that i dont know how to get the notification when the app is killed.
My idea was to call the service if the badge icon was great than 1, in this way i know that there are some notification to download and i can contact the server to get them.

A silent push notification doesn't need the alert, either the sound to be specified, since it's not presented to the user.
The silent notification you will get through application:didReceiveRemoteNotification:fetchCompletionHandler::
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
//load the new available content and call the completionhandler.
}
The badge should be displayed if the app has the correct permissions. You can register your app to display the badge like this:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge];
Anyway you don't have to check the badge number to download new content. You can simply attach your own key-value-pairs to the notification:
{
"aps": {
"badge": 33,
"content-available":1
},
"load-data": 1,
"load-data-id": 12
}
When you receive the notification in the app just check for your param in the userInfo dictionary:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
int loadData = [[userInfo objectForKey:#"load-data"] intValue];
int loadDataId = [[userInfo objectForKey:#"load-data-id"] intValue];
}
All of this is documented in the Local and Push Notification Programming Guide in the iOS documentation.

Related

What is UIApplicationLaunchOptionsRemoteNotificationKey used for?

if I understand correctly, the UIApplicationLaunchOptionsRemoteNotificationKey key is used on the -[UIApplicationDelegate application:didFinishLaunchingWithOptions:] method when
- the push was received when the application was not running (e.g. killed)
- the user clicked on the received push
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if(userInfo) {
// app was not running and the user clicked on the push
}
}
but .. in this exact same case, the -[AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] is also called just after the previous one.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
// called when
// app was not running and the user clicked on the push
// app was running in background and user clicked on a push
// app was running in background and a silent push was received
// app is in foreground and a push is received
completionHandler(UIBackgroundFetchResultNewData);
}
So the question is, why should I use the UIApplicationLaunchOptionsRemoteNotificationKey if everything can be handled in the application:didReceiveRemoteNotification:fetchCompletionHandler delegate? Did I miss something?
cheers,
Jan
In case when the app is killed and user taps on push notification in notification center, launchingOptions dictionary contains UIApplicationLaunchOptionsRemoteNotificationKey so that you can adjust your app start logic.
In prior iOS version there wasn't application:didReceiveRemoteNotification: fetchCompletionHandler: and launchingOptions dictionary from application:didFinishLaunchingWithOptions: was the only place where you could handle remote notification on app start.
My guess is that application:didFinishLaunchingWithOptions: contains UIApplicationLaunchOptionsRemoteNotificationKey for compatibility reasons.
The presence of this key indicates that a remote notification is available for the app to process. The value of this key is an NSDictionary containing the payload of the remote notification.

Push notification data not getting when app launched directly by clicking app icon

I have a scenario in which app will get push notification and need to show that messages in home screen of my app, for that i saved the message array into user defaults from my app delegate and everything works well, but in following conditions it's not working
if app is in killed state and a notification came and user triggeres the app through the app icon (not from push notification )
if app is in background and notification came and user enters to app through app icon (not from push message ) in this case also
Then i searched for solutions and came to know about silent push notifications (for background mode) and nothing else so i need to know how to handle all scenarios by push notifications and my appdelegete is
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
[self handlePushMessage:remoteNotif];
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[self handlePushMessage:userInfo];
}
-(void)handlePushMessage :(NSDictionary*)userInfo{
//method to handle push message
}
Thanks in advance
This is a common issue: if the user does not open your app by means of the displayed notification, there is no* way of getting the related information.
* A possible solution employed by many apps is to check with a remote server for unread notifications (e.g. check for an unset read-date field).
In scenario 1. NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; here remoteNotif return nil as you enter into the app through triggering app icon.
In scenario 2. You can get push notification info through the following method
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
if (userInfo) {
[self handlePushMessage:userInfo];
}
}
if app is in killed state and a notification came and user triggeres the app through the app icon (not from push notification )
If the app is in killed state then the push notification payload can only be handed over to the app when the user taps the push notification itself. If the user launches the app from app icon then the notification payload will not be passed to the application
if app is in background and notification came and user enters to app through app icon (not from push message ) in this case also
if you are targeting iOS7 and up then you need to enable background mode for remote notifications. Please check below link in order to get notification payload even when app is in background
didReceiveRemoteNotification not working in the background
The above mentioned app delegate method gets called when the app is in foreground,Background and suspended state.
There is no way to get the notification payload when app is killed and when the app icon is directly clicked instead of push notification in notification center.
Enable "Remote Notifications" under background modes in capabilities in target settings. This will fetch the notification data even when the app is in the background. Also, make sure to implement:
-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler;
in your app delegate.

Get push notification data when application is in background IOS

Hi I am developing small IOS application in which I am using push notification. So in my case I am able get notifications and I am also able to access data for notification. Data from notification mean title, description etc. So in my case I am not saving my notification at my server side. I want to save those locally. For that what I want as soon as notification come I want to save that data locally. I am able to access my data when App is in foreground but I am not able to access my notification data when app is in background. I want to access the data of notification so that I can save it. For notification I did following things:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
return YES;
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken {
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
for (NSString *key in [userInfo allKeys])
{
NSString *data = [userInfo objectForKey:key];
NSLog(#"inside did register for notification .... %# ---- > %#",key,data);
}
}
My requirement is simple I want access to notification when my app is in background. Any one is here who already did this? Need Help. Thank you.
In iOS, the app cannot access it's push notification until the user taps on the push notification from the notification center.
Once the push notification is tapped and the app loads/becomes active, only then will you be able to access the push notification.
FYI:
When the app is in background and a push notification is recieved.
After the user taps on the push notification:
the contents will be accessible in the -didReceiveRemoteNotification: method.
When the app is not open and a push notification is received.
After the user taps on the push notification
the contents will be accessible in the -didFinishLaunchingWithOptions: method in it's launchOptions parameter.
Also... push notifications aren't 100% reliable. They may or may not be delivered (although them not being delivered is pretty rare in my observation but worth pointing out none-the-less)
There is a way in iOS 7.0 and later where you can send notifications and access its content without user tapping it.
So, the notification payload which you send has a parameter called content-available. You can view the payload parmeters in the following link:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
So when you set the content available to 1 and deliver the notification, iOS calls the below function even if the app is not in the background or foreground(must have remote notification enabled)
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
Then you can use the userInfo dictionary to fetch your notification data.

Getting push notification payload when user opens app manually after push has been received in the background

I am using iOS7 and I am trying to determine if I can get the JSON payload in the following situation.
I have background mode "remote-notifications" enabled
The push notification is received while the app is terminated
The app is launched manually from the icon not from the notification center
When I launch the app from the icon itself after the notification has been received I do not get the push in the launch options from
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
and the following method does not get called either when app is manually launched from the icon
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
I finally figured out how you can get this!
As of iOS 7 you can get it!
Basically, you need to configure your application for background remote notifications.
So, in your info.plist file:
For required backgrounds - set it to app downloads content from push notifications.
In the AppDelegate.m file, you need to implement this method:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
See this for how to implement that: didReceiveRemoteNotification: fetchCompletionHandler: open from icon vs push notification
For your push notifications, you must have 'content-available': 1, as part of the push notification. This is what tells the application that there is new content before displaying the alert.
See this page for more information on background remote notifications: http://developer.xamarin.com/guides/cross-platform/application_fundamentals/backgrounding/part_3_ios_backgrounding_techniques/updating_an_application_in_the_background/

Apple Push notification not deliver on ios 7 when application remove from background

I have tried silent Apple push notification in ios 7 using following code. The Push notification received when application is in foreground or in background. Then I remove application from background by swiping the application from background apps. After that If I send a Push notification from my server, it sent to APN properly, but not delivered to iPhone.So Apple push notification in ios 7 delivers silent push notification only application running in backgroud? If user remove application from background will it receive notification or not?
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSString *receivedMessage = [[userInfo objectForKey:#"acme1"] objectForKey:#"mydata"];
NSLog(#"fetchCompletionHandler receivedMessage -> %#",receivedMessage);
completionHandler(UIBackgroundFetchResultNewData);
}
didReceiveRemoteNotification not called when app will not run,it only called when app will open and foreground.
you will get list of notifications in UILocalNotification,if app not run and it receive notifications like this :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
.........
.........
.........
UILocalNotification *localNotif =[launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSLog(#"****** notifiation ******");
}
return YES;
}
App will be able to receive the notification even when app is closed i.e. not in background mode.

Resources