get push notification while the app is not running iPhone - 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];
}

Related

how to retrieve and handle remote push notification content in iOS app

I was trying to fetch remote notification info when the app was not running,so I was told that I can get from :
UILocalNotification *localNotification = [launchOptions
objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]
in method:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
,but still can't get info.
Then I use alertView to show the info on iPhone(launch without Xcode),still can't get the info.
Any other issue would cause this? Please let me know if you have any ideas.
How to retrieve and handle remote notifications:
app is running
The userInfo in below method already includes the push notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
}
app not running
The value for key in launchOptions includes the push notification,under two circumstance:
1.screen is locked,when receive the remote push notification,screen is lighted and user unlock the screen then directly launch the app.
2.user tap on the notification on the drop-down menu to launch the app.
If user tap on the app directly,then the notification will be gone and missed.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey] description];
}
That key is only in the launch options when the user starts your app from the notification (e.g. taps on it in notification center). Incidentally, I don't think a remote notification would be the class you're using (UILocalNotification).
If your app wasn't in the foreground when the device received the push, and the user didn't launch your app from the notification, the notification is gone. You have to check your own servers to see if you missed anything.

Clear push notification badge after increment

I am working on push notification in iphone. when i receive push notification, its showing 1 on my application icon, next time its 2,3,4. if i open application its 0. Next time its should be 1,2,3,4... but its showing last number and +1. i want reset push notification badge after open application. i am sending +1 from urban airship.
and its not working for me.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
I use this code in my app because the Urban Airship (UA) documentation said to
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
[[UAPush shared] resetBadge];
but it doesn't work, the badge on the app icon keeps incrementing. I saw a few posts on this very issue on UA's forums and they haven't given a clear answer.
EDIT #1:
I received the following response from a support technician at UA with the following suggestions, which worked great:
What you want to do, is ensure that in your didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method, you are calling the following:
[[UAPush shared] setAutobadgeEnabled:YES];
[[UAPush shared] resetBadge];//zero badge on startup
and also call [[UAPush shared] resetBadge]; in the following methods as well:
applicationDidBecomeActive:(UIApplication *)application
and
didReceiveRemoteNotification:(NSDictionary *)userInfo
The technician also mentioned that assigning 0 to applicationIconBadgeNumber is not necessary, so I took it out. Still works beautifully.
EDIT #2:
I ended up having to modify application:didReceiveRemoteNotification: to include a call to UA's handleNotification:applicationState: method:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// Get application state for iOS4.x+ devices, otherwise assume active
UIApplicationState appState = UIApplicationStateActive;
if ([application respondsToSelector:#selector(applicationState)])
{
appState = application.applicationState;
}
[[UAPush shared] handleNotification:userInfo applicationState:appState];
[[UAPush shared] resetBadge];
}
because I was having a problem with the following scenario:
User is in app
Push notification received
No badge was displayed on app icon when returning to home screen (as expected)
Another push notification is received
Badge displayed number greater than 1
With the modification above, this scenario is handled. I guess you have to tell UA that the notification is handled when one is received and the app is running in the foreground.
I have used Urban Airship and have had this problem before. You are not showing the code but i am assuming that when a notification is received, you are setting the application badge number to what urban airship is passing to you, don't do that. Just let the application self handle this, when a remote notification comes in, let it auto increment on its own. If that is not the case it could be that, on the urban airship side, you are setting a badge number to send with the push. Do not send a badge number with the push notification, leave that part out, iOS should auto increment your badge from its current badge number.

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.

Handling Push notification

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.
}

Problem With PushNotification if you select No at the first time

As i said in the title of this thread, I have a problem with Push notification of my application.
There is 2 cases :
When the app asks you if you want push notification and you push OK, it's working great.
This function :
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
is called when registerForRemoteNotificationTypes is called :
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Add registration for remote notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
...
}
But when you select NO at the beginning, and then you change your settings to accept push notification, nothing append and this function didRegisterForRemoteNotificationsWithDeviceToken is never called.
Please help me, i'm getting crazy!
I see you register for remote notifications in:
- (void)applicationDidFinishLaunching:(UIApplication *)application
If your application is running on a iOS device with support for multitasking that might not be the right place.
The above method is called when you initially launch your app from cold. This is when you are prompted to accept or decline Push Notifications - and in your case decline.
When you later exit the application to change the settings in the device wide control panel the application is not quit - it is merely resigning as the foreground application. When you have enabled Push Notifications in the control panel and return to your application - the application is not launched again but moves from being backgrounded to the foreground.
Your application delegate will at this point receive an:
- (void) applicationDidBecomeActive:(UIApplication *)application
try registering for Push Notifications there instead using:
- (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types
You could also prevent backgrounding by setting the “Application does not run in background” property in the application’s Info.plist file (UIApplicationExitsOnSuspend).