Alert sequence when Push Notification comes in - iphone

Please help me understanding this:
When home button is pressed and iPhone application goes in the background and a Push Notification is received. There is an alert being displayed which contains the message for that notification with "View" button on it. Where this alert is coming from -- IOS generating it?
In my code I wrote code for showing alert when notification comes in inside my
- (void)application:(UIApplication *)iApplication didReceiveRemoteNotification:(NSDictionary *)iUserInfo {
method. And on this alert action I am showing my view controller. Now, I am ending up showing two alerts -- One which is coming from IOS (I believe) and on tap of view is taking me to last visited page of my app and Second which I created and on tap of View is taking me to desired page.
Please help understanding this.

If I understand you correctly, you get two AlertViews instead of one if you click on View. Then you should check whether the application is active or not. Have a look at the UIApplication Class Reference
UIApplication Class Reference #applicationState
- (void)application:(UIApplication *)iApplication didReceiveRemoteNotification:(NSDictionary *)iUserInfo {
if([application applicationState] == UIApplicationStateActive) {
//show alert
}
}

Oliver Drobnik wrote a very detailed examination of the possible message flows when dealing with notifications and app state. The short version - if your apps not frontmost, the OS displays a notification and may launch your app in response to user actions; if your app is frontmost, you're responsible for everything, be it displaying an alert or some other processing.
http://www.drobnik.com/touch/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/

Related

Can we lock the app while app goes to sleep mode in IPhone?

I am working an app where lock option is included.My app starts with passcode screen.If I enter correct code then it navigates to next screen.If I dont use the app for long time it goes to sleep mode.When the user wants to run the app now, the passcode screen should appear and the user has to enter the code again.Is it possible?Is there any tutorial for this?Please dont mind to post the related code if you have done it.Thanks in advance.
Yes ofcourse it is possible. You must open the screen in a method called applicationDidBecomeActive in your Application Delegate. This method is called every time the application is opened from background.
So whenever the user starts the already running app, this method will be called and from this you can first show the Password screen, and after that the respective screen.
You can detect when your app goes to the background using the UIApplicationDidEnterBackgroundNotification. When it does, record the date and time. When the user opens the app back up, you will receive UIApplicationWillEnterForegroundNotification. When you receive that, compare the recorded date and time with the current date and time. If that's too old, display the passcode screen.
check in app delegate class there the methods applicationDidEnterForeground and applicationDidEnterBackground are available do your coding there
I have developed same type of apps, where I have implemented this things, For this I made a one Class like this
#interface CommonUIClass:NSObject
+(void)setCurrentViewController:(id)controller;
+(void)openPassWordProtectedScreen;
#end
And
#implementation CommonUIClass
static id currentViewControllerObj;
+(void)setCurrentViewController:(id)controller{
currentViewControllerObj = controller;
}
+(void)openPassWordProtectedScreen{
PROTECTED_CONTROLLER *view = [[PROTECTED_CONTROLLER alloc]init];
if ([currentViewControllerObj respondsToSelector:#selector(presentModalViewController:animated:)]) {
[currentViewControllerObj presentModalViewController:patternLock animated:NO];
}
}
#end
Just import this class to every ViewController And put this code to
-(void)viewWillApear{
[CommonUIClass setCurrentViewController:self];
[super viewWillApear];
}
And When Application Goes in Background
-(void)applicationWillResignActive:(UIApplication *)application{
[CommonUIClass openPassWordProtectedScreen];
}
Thanks..

iOS UILocalNotification - No delegate methods triggered when app is running in background and the icon is clicked upon notification

iPhone version - 5.1 (9B176)
Below is the series of events during Local Notification where in which didFinishLaunchingWithOptions method is not invoked.
App is running in background.
Got local notification - simple notification no dialog.
Click on the app icon which shows the badge number.
Expected as per Apple documentation:
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 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
Actual :
didFinishLaunchingWithOptions NOT invoked. But applicationWillEnterForeground and applicationDidBecomeActive were invoked.
You are correct. The behavior is inconsistent with the documentation. Putting the documentation aside and focusing on actual behavior; The crux of the matter seems to be this: If your app becomes active by the user interacting with the notification you will receive a pointer to the notification, if the user taps your application icon directly you will not.
To illustrate. If you present an alert style notification and the user taps the action button, or if, as in your case, you present a banner notification and the user taps on that you will receive a pointer to the notification in one of two ways:
If your application was in the Not-Running state:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
UILocalNotification *launchNote = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (launchNote){
// I recieved a notification while not running
}
}
If your application is running in any state:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
// I recieved a notification
}
In the case where a user elects to cancel an alert style notification, that notification is gone.
The truly annoying an inconsistent part is that if you present a banner notification and the user taps your icon you seem to have no way of retrieving a reference to the presented notifications in the notification center. i.e. they do not appear in the [[UIApplication sharedApplication] scheduledLocalNotifications] array, presumably because they are no longer scheduled but are now presented.
So in short; The documentation is wrong. And there are other annoying inconsistencies. If this behavior is a problem for you you should file a bug with Apple.

When notification fires, "slide to view" shows a specific controller. Is this possible?

I have an app in which I'm using push notifications. What I really want to do is when a user slides to view from the lock screen he should be able to go to a specific view controller, and not just simply open the app.
Is this possible? Can anyone direct me towards right direction?
Thanks a lot!
In your application:didFinishLaunchingWithOptions: method you can check whether your app was started due to a remote notification by looking into the launchOptions dictionary. The key UIApplicationLaunchOptionsRemoteNotificationKey will give you the remote notification, if any, and you would then need to present your view controller.
If your app is still running while the remote notification arrives your app delegate's application:didReceiveRemoteNotification: method is called.
See the UIApplicationDelegate documentation.
The app needs to figure out and show the right view controller when it is opened after a push notification.

How do you register for UIApplicationWillEnterForegroundNotification in MonoTouch

I have a problem that the ViewWillAppear method for a UIView does not fire when the application returns from the background. This is a problem, as my main application screen is showing values that are retrieved from the user settings, and if the user has changed these while the application was in the background I need to have the screen refreshed. I have read that one should register for the UIApplicationWillEnterForegroundNotification using NSNotificationCenter.
How do you do this in MonoTouch? Or does anyone know an alternate way of ensuring that the screen is always kept up to date, even when returning from the background?
You could try something along the lines of:
//Register for the notification somewhere in the app
NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.WillEnterForegroundNotification, EnteredForeground);
//snip
void EnteredForeground (NSNotification notification)
{
// do your stuff here
}
Bear in mind you would need to do this for every view controller you'd like to update when enterting from the background!

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