Handling Push notification - iphone

I am working with push notification, when the application is opened/or in backgroung process then it is no problem for me to receive the push notification, the problem occurs when i quit my application, it is not calling my delegate. Why?

When an app is launched from a push notification, the code path is a little different. Instead of getting the -application:didReceiveRemoteNotification: message, your app delegate gets some extra information passed into its -application:didFinishLaunchingWithOptions:. You can get the notification dictionary out of the options one and hand it off to your regular push-notification handler, like this:
- (void)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)options
{
NSDictionary *pushDict = [options objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(pushDict)
{
[self application:app didReceiveRemoteNotification:pushDict];
}
// etc.
}

Related

didReceiveLocalNotification: method works only if I press on notification banner

I have some UILocalNotifications in my app. When I receive one while I am in the app or if I press on the notification banner from the block screen or when it appears, the didReceiveLocalNotification works just fine. However, if I do not use the application and if I press on the icon of it (not on the banner) after notification banner appeared, the didReceiveLocalNotification method is not being called.
What should I do in this case, if I still want receive information about local notifications? Can I get it somehow in the applicationDidBecomeActive: method for example?
If you're app is completely shut down and you open it with a local notification, your local notification info will be passed into application:didFinishLaunchingWithOptions:.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification)
{
// Do whatever
}
// ...
}

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

Push notification data retrieving

I want to get data from push notification message. I successfully get the data when app is on foreground and in background. but I am unable to get data when app is quit and user press view button on push notification. I write the code in application did finish launching. This code cause the app crash when pressing on View button of push notification message. If I comment the code then app doesn't crash. Kindly help me to fetch data from push notification when app is quit and user press view button on push notification. I'll really appreciate that.
if(launchOptions != nil){
NSDictionary *tmpDic = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (tmpDic!=nil) {
pushedMessage=[NSString stringWithFormat:#"%#",[[tmpDic objectForKey:#"aps"] objectForKey:#"alert"]];
pushedCountry=[NSString stringWithFormat:#"%#",[tmpDic objectForKey:#"country"]];
[self saveToDatabase];
}
}
Please try this...
Add this code to appdelegate.m => didFinishLaunchingWithOptions
if ([launchOptions valueForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"]) {
[self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}
From ios7 we have the below delegate method to handle the push notification when the app is in back ground or not running
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
When you click view button
- (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo
{
}
this method is called and userinfo will contain all data
Things which you are doing in didfinishlaunch method do in didReceiveRemoteNotification:
I don't totally understand what your asking but you can do stuff with what is being being push with a function in the app delegate
- (void)application:(UIApplication*)application didReceiveRemoteNotification:
(NSDictionary*)userInfo
{
NSLog(#"Received notification: %#", userInfo);
[self addMessageFromRemoteNotification:userInfo updateUI:YES];
}
Now you can add that data to core data or sqlite. This may not be relevant to your question but it's the best I can give based on what you've asked in your question.
See my comment on the above answer.
Here is Apple's doc:
If the app is not running when a push notification arrives, the method
launches the app and provides the appropriate information in the
launch options dictionary. The app does not call this method to handle
that push notification. Instead, your implementation of the
application:willFinishLaunchingWithOptions: or
application:didFinishLaunchingWithOptions: method needs to get the
push notification payload data and respond appropriately.
So you need to implement:
- (void)application:(UIApplication*)application didReceiveRemoteNotification:
(NSDictionary*)userInfo
As well as handling the launchOptions in:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

get push notification while the app is not running iPhone

I am working on one iPhone app which involves a push notification. As I have seen in many documents and tutorials it suggests to register for push notification with in
application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
like the following:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert| UIRemoteNotificationTypeBadge| UIRemoteNotificationTypeSound];
...
}
now the question is, if the app was not running (even in background), when the push come, it cant process the push message, but if I use the push message again from the notification area and lunch the app again, I can get my message.
what I need to do to make my app get the push message even when it lunch for the first time?
You might be conflating the notion of registering for and receiving notifications. It is impossible for an app to receive a push notification before the registerForRemoteNotificationTypes: method is called the first time, since this method provides the push token that is used to send push notifications in the first place.
So, you must be talking about receiving notifications under the two separate situations in which they can be delivered: upon initial app launch, and during the execution of the program.
In order to handle notifications of the first type, you must inspect the options dictionary sent to application:didFinishLaunchingWithOptions:. The following code shows how to route a notification received at launch to the delegate method that is called when a push notification arrives while the app is already running.
Place this in your application:didFinishLaunchingWithOptions: override:
NSDictionary *pushNotificationPayload = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(pushNotificationPayload) {
[self application:application didReceiveRemoteNotification:pushNotificationPayload];
}

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.