Launch application from alert view of push notification - iphone

How can i launch my application when user taps the view button in the alert that came due to push notification?

It happens automatically. When the user taps the "View" button, your application will launch and your application delegate will receive the -application:didFinishLaunchingWithOptions:. If your application's already running and in the background, I believe it receives the -application:didReceiveRemoteNotification: message, probably just after it gets -applicationWillEnterForeground:.

Related

iphone development: launching app on receiving push notification

In my app I can successfully receive push notifications. However, if the app is in the background, when I open the app I faced with the last state of the app. What I want is, even if the app is on background, opening the notification should relaunch the app like it is the first time the app is opening. Is that possible?
In your AppDelegate you could react to the receipt of the notification and restore the application state in a way that is appropriate for your app.
For example if your main view controller is a UINavigationController and the user may have navigated down in the view hierarchy, you could just pop back to the navigation controller's rootViewController:
[[self navigationController] popToRootViewControllerAnimated:YES];
I don't know if you could relaunch the app programmatically but if you open the app from a notification the
application:didReceiveRemoteNotification:
from the UIApplicationDelegate gets called, then you could do there whatever you want to do

Get custom payload of push notification while app is running background

Hey can anybody tell how to get custom payload of push notification when app is in background?
If your app is in the background and you receive a push notification and the user taps app "View", the application is launched and application:didFinishLaunchingWithOptions: would get called. In this case we can access custom notification payload via UIApplicationLaunchOptionsRemoteNotificationKey from userInfo dictionary.
If your app is in the background and you receive a push notification and the user taps app icon, it brings the application to the foreground.
When that happens, only applicationDidEnterForeground: is called, and there is no way to access the payload of the push notification.
My requirement is to register for Badge only, no alert so no 'View' button to launch app when user receives notification in background. When user receives notification and application is in background, user taps app icon applicationDidEnterForeground: is called. So we have no way to access launchOptions dictionary or userInfo dictionary from where we can get notification payload.
Am I missing something here?
application:didReceiveRemoteNotification: is called when the user taps on the notification in the statusbar drop down if your app is running in the background. The link you referred to in your answer explains how to use it.

Which delegate method gets called when using Push Notifications if application is in background state?

Reading from Apple's documentation about Push Notifications:
As a result of the presented notification, the user taps the action button of the alert or taps the application icon.
If the action button is tapped, the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method
The notification is delivered when the application is running in the foreground.
The application calls its delegate’s application:didReceiveRemoteNotification: method
So my question is which delegate gets called if the application is in background state (it is running or it's suspended)? Is it application:didFinishLaunchingWithOptions: or application:didReceiveRemoteNotification:?
Please help me, thank you!
application:didReceiveRemoteNotification: is called when your app is in the background. This question has an answer which tells you how to tell if your app was in the background or not.
application:didReceiveRemoteNotification: is called when your app is in the background + the message alert still active.
Once the message alert is inactive then the application will not receive any event. Do correct me if I'm wrong.

Can i get the push notification's custom properties when the app is closed or background running?

in iphone,can I get the push notification's custom properties when the app is closed or background running?
When an app(not running) received a push, system generate an alert with two buttons: "Close" and "View". If the user taps View, the application is launched. my question is can i change the action of the "View" button, such as open a web link?
I'm not sure that understood what you need...
Think isn't possible... Only things you can change is button (and body) text. More info you can find in Table 3.2. One of the solution could be, to automatically open a web link, when application is launched from push notification.
If the action button is tapped, 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, the application calls the same method, but furnishes no information about the notification.

Deferentiating the push notification handler when application is foreground and background

It is said that (correct me if I'm wrong) if the application is in the foreground we have to handle push notifications in the "didReceiveRemoteNotification" and if the application is in the background using "didFinishLaunchingWithOptions" when user taps the "view" button of the app. As I dont have a phone to test I want to know whether I am handling this properly.
1) What will be invoked when I taps on the "View" button in the push notification?
2) Let say I am running the application in the foreground and push notification receives at the same time. Will I be given the push notification alert? If so what will happen if the user click on the View button?
3) In this thread How to handle push notifications if the application is already running? it says:
"alert" key will not be there directly under the userInfo dictionary, you need to get another dictionary with name "aps" and then get the "alert" or "body" from "aps" dictionary"
Is this true?
4) I need to push to a certain view when the user clicks on the View button. Hence do I need to handle that code in both methods?
Thank you
There's a nice rundown of the methods invoked by a push notification in this Apple vid: http://developer.apple.com/videos/iphone/#video-advanced-pushnotification - make sure you visit download the full version in iTunes.
This direct link might work: http://developer.apple.com/itunes/?destination=adc.apple.com.3391495696.03391495702.3416205190?i=1378617410
Either way, the general idea is that if your app isn't in the foreground, tapping your view button will trigger didFinishLaunchingWithOptions, and if it is the foreground app, you'll get the didReceiveRemoteNotification.
I don't think you'll get the alert. The method didReceiveRemoteNotification will be called, and it'll be up to you to show a UIAlert if you want.
Yes - that's true.
Yes, but I think you can simplify this by creating a third method specifically designed to handle your view. You can call this from both didFinishLaunching (only if it launched via a notification), and didReceiveRemoteNotification. This way, if your app needs to be launched, you can have time to do any other setup you might need to do for the app to work right out of the get-go (load saved data, init tabbar controllers or anything else like that).
Best of luck