Missing push notifications - iphone

My iPhone app's delegate has push notifications all set up:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
for (id key in userInfo) {
NSLog(#"key: %#, value: %#", key, [userInfo objectForKey:key]);
}
}
Now, I launch it, then suspend by pressing the home button. I then send a notification to the device, and the notification is received. A dialog appears with two buttons - "Close", and "View". Now, when I click "View", the app gets resumed, and didReceiveRemoteNotification is sent to the delegate.
But if I click "Close", the app remains in the background. If I then manually launch the app, neither "didReceiveRemoteNotification", nor "didFinishLaunchingWithOptions" are sent. So - where is my payload? How do I get the payload in this case?

If the user closes the notification and launches the application using the app icon you won't get the payload anymore.. it is lost forever then.
This is described at http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html in the "Handling Local and Remote Notifications" section.
Also notice that the server only saves one notification for your app for a user. If the device is offline and your notification is not received but you send another one the first one will never reach the user because it replaces the first one.

Related

iOS Push notification issue

I am doing a project in which push notification feature is one of the key feature.
It is working fine when I am in the app, I receive notification and handle that notification.
But the issue is when I am in background and notification receives I see badge on my app icon
and when I click on the icon my app is launching but the didReceiveRemoteNotification method is not called so I am unable to handle that notification.
And another issue is some times it shows the notification message in device notification list and some times it didn't .
When I am entering in my app through clicking on notification list item the didReceiveRemoteNotification calls and I am successfully able to handle notification.
I write following code in didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method
NSDictionary* remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif != nil)
{
NSLog(#"didFinishLaunchingWithOptions\nNotification recieved:\n%#",remoteNotif);
notificationData=[[NSDictionary alloc]initWithDictionary:remoteNotif];
[notif saveNotification:remoteNotif];
}
Help me to resolve this .
Thanks in advance.
Things which you are doing in didfinishlaunch method do in didReceiveRemoteNotification as well.
When you will come from background, didfinishlaunch won't be called.
- (void)application:(UIApplication*)application didReceiveRemoteNotification:
(NSDictionary*)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
//What you want to do when your app was active and it got push notification
}
else if (state == UIApplicationStateInactive)
{
//What you want to do when your app was in background and it got push notification
}
}
From Apple's Local And Push Notification programming guide
Handling Local and Remote Notifications
Let’s review the possible scenarios when the system delivers a local
notification or a remote notification for an application.
The notification is delivered when the application isn’t running in the foreground.
In this case, the system presents the notification, displaying an
alert, badging an icon, perhaps playing a sound.
As a result of the presented notification, the user taps the action
button of the alert or taps (or clicks) the application icon.
If the action button is tapped (on a device running iOS), the system
launches the application and the application calls its delegate’s
application:didFinishLaunchingWithOptions: method (if
implemented); it passes in the notification payload (for remote
notifications) or the local-notification object (for local
notifications).
If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information
about the notification . If the application icon is clicked on a
computer running OS X, the application calls the delegate’s
applicationDidFinishLaunching: method in which the delegate can
obtain the remote-notification payload.
The notification is delivered when the application is running in the foreground.
The application calls its delegate’s
application:didReceiveRemoteNotification: method (for remote
notifications) or application:didReceiveLocalNotification: method
(for local notifications) and passes in the notification payload or
the local-notification object.
So in your case, when application is running in background, and when you click the notification/alert, operating system brings your app into foreground. So it falls under second point.
You can implement application:didReceiveRemoteNotification: method to get the notification payload, if action button is tapped. But when the application icon is pressed instead of action message, the notification payload is not forwarded with the method. Your only option is to contact your server and sync the data. After all as per Apple's policy, push notification only tells that there is data on server, and either way you need to connect to server and get the data.
I did the same thing in one of my application. There is no way to handle notification when clicked on app icon. So, what you can do is to make a server call to get latestPushNotificationIdSync.
You must be storing your data somewhere on server so, you need to check on your server what is the latest latestPushNotificationId.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
notificationContentDict = launchOptions;
if([[launchOptions valueForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"]count]>0){
application.applicationIconBadgeNumber = 0;
NSString *key = [[launchOptions valueForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"] valueForKey:#"id"];
contentId = key;
////// you can use this content id ///// write your code here as per the requirement ..////
//// display your content on UI /// either get from server or local ...///
[self displayContent:[launchOptions valueForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"] application:application];
}
else
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(#"badge count ..%d", application.applicationIconBadgeNumber);
if( application.applicationIconBadgeNumber >0){
if(!notificationContentDict){
make a server call to get "latestPushNotificationIdSync"
application.applicationIconBadgeNumber = 0;
NSLog(#"badge count applicationDidBecomeActive.%d", application.applicationIconBadgeNumber);
}
}
}

iOS Push Notification - How to get the notification data when you click on the app icon instead of notification

Similar to this question: How do I access remote push notification data on applicationDidBecomeActive?
But the different is how can you access the notification data when you are inapplicationDidBecomeActive and if you have clicked on the app icon instead of the push notification.
The flow is: If you click on the push notification then didReceiveRemoteNotification will be triggered, but if you click on the original app icon, only applicationDidBecomeActive will be triggered and didReceiveRemoteNotification will not be called.
I am looking for the later case so how can I access the push notification data.
(Both case assuming the app is in background and not killed yet.)
You can't get remote push payload by launching app from homescreen.
If the push data is important for app use, load it from your server after app launched.
#fannheyward answer is absolutely correct. You cannot get payload when application is launched by tapping app icon.
I have an idea, what if you get to know that some notification is pending when app is launched by tapping app icon. With this knowledge your app can fetch payload from your server.
You can set "Badge" in every such notification and on applicationDidBecomeActive you can check [application applicationIconBadgeNumber] > 0 to know that some notification is active. After fetching payload from your server you can set it to 0 like below
[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
Please note: This means your app will have badge displayed over it when notification is received. I am not sure of the behaviour when badge is disabled by user from settings.
If your application target is over iOS7, you can do only if application is alive in backgroud.
In the capabilities settings at Xcode, you should enable Background Modes>Remote notifications, and write below code.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// save userInfo in NSUserDefaults
completionHandler( UIBackgroundFetchResultNoData );
}
If you want to test it, it will be helpful to use https://github.com/acoomans/SimulatorRemoteNotifications
From the server side, make sure to set content-available property with a value of 1
For this to work I also had to check the background fetch box.
You should get the notification in the launchWithOptions method in your appDelegate something like this:
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteNotif != nil){
//Handle your notification
}

iOS 5 banner push notification disappear, user tap on app icon to start the app

With iOS 5, push notifications can appear as banner and disappear after a few seconds.
I understand that didReceiveRemoteNotification will be called when user taps on the banner.
My question is, if the banner has disappeared and my user sees that there is a badge number on the app, they will tap on the app icon to start the app. Now if the app is running in the background, how do I check that the app is brought to foreground and there has been a notification, and do the necessary?
The purpose of my notification is basically to inform user there has been an update to the app content and encourage them to run the app to get the latest contents. My app only checks for latest contents at launch time and doesn't check for updates periodically.
This question is a bit old, but I'll pop what I've found in here anyway.
There are two methods you need to implement in your app delegate to check if your app was either launched from the remote notification (From when the app is not running on your device), or received the remote notification while running (in the background or foreground).
First is a method that is already in your App Delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
To check if this was launched from a remote notification, have some code similar to this:
// Check to see if launched from notification
if (launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSLog(#"Launched from push notification: %#", dictionary);
// DO SOMETHING HERE
}
}
The other method you will need to implement is specifically for the case that your application for when your application is running:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"Received notification: %#", userInfo);
}
How you handle the notification from there is up to you, but that's how your app knows about it!
In this second method, you can check the UIApplicationState of the passed application to find out if you were in the foreground or background.

Can i get info about notification (apn)

Can I get list of push notifications (APNS notifications) for my app when app became from background to foreground mode?
In foreground mode i can receive info about push notification in callback
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
Other case:
My app receive push notification, when app in background mode.
After this i click on app icon, and i want to get info about received notification. How can i get this info?
If i click directly on the notification (not on app icon), in background mode, then callback didReceiveRemoteNotification is call.
Can I get list of push notifications (APNS notifications) for my app when app became from background to foreground mode?
No. There is no list. You can only get one notification at once. When the users iPhone is offline and you send 5 notifications the user will only get the last one you have send.
If the user starts your app using the open action on the notification you will get it on start using:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Other Case
My app receive push notification, when app in background mode. After this i click on app icon, and i want to get info about received notification. How can i get this info?
You can't. When the user closes the notification and opens you app later it is already gone and there is no way to access it.
When you send push notifications you probably have a server reachable over the internet, where you register the devices of the user.
The usual way to handle this is to store the notifications on this server and query it on app launch... so use the notification just to notify the user to start your app and then check your server on launch of the app for whatever you want.
Once your application reaches foreground and actively running, the notification alerts like sound, alert won't be displayed or you wont be notified.
But you will get a call back in the UIApplication Delegate that you can make use of it.
The api is,
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
And, if your application is running in background, then the notification appears and only when you click on "View" button, you will get the call back in UIApplication delegate.
If you click on Close button, you won't get the call back in the application.

Push Notification Problem

I want to get the reminder at each notification comes from the web service in the application for the different tasks. For that what i have used the following methods.
but i am not getting push notific
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
Receiving push messages in the app
So what happens when your iPhone receives a push notification? There are three possible situations:
The app is currently running in the foreground. Nothing happens on the screen and no sound is played, but your app delegate gets a notification. It is up to the app to do something in response to the notification.
The app is closed, either the iPhone’s home screen is active or some other app is running. An alert view pops up with the message text and a sound is played. The user can press Close to dismiss the notification or View to open your app. If the user presses Close, then your app will never be told about this notification.
The iPhone is locked. Now also an alert view pops up and a sound is played but the alert does not have Close/View buttons. Instead, unlocking the phone opens the app.
I ran into a lot of problems on Push notification. Please note: if you are not getting the device token back, check to make sure the device is connected to the internet. If not device cannot contact the APN server. It took me days to find this out