iPhone how to access the notification center programmatically - iphone

As per the Apple guide:
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
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html#//apple_ref/doc/uid/TP40008194
As far as I have known, it seems not be able to detect the notification when the application icon is tapped.
So I tried to retrieve the notification center programmatically but it also seems to be impossible.
Is it impossible to retrieve the notification center programmatically?
What I want to do is detecting whether the notifications received or not even when the application is in the background.

Is it impossible to retrieve the notification center programmatically?
No, it's not possible with any public API.
Your app is agnostic about the current state of the Notification Center, as they are two decoupled entities.
Anyway, as noted by AdamG, in iOS 7 you can implement
application:didReceiveRemoteNotification:fetchCompletionHandler:
which, according to the documentation, is called regardless of the state of your app (so even when it's not running or in background).
In order to use it, you have to support the remote-notification background mode. Here's how:
In Xcode 5 and later, you declare the background modes your app supports from the Capabilities tab of your project settings. Enabling the Background Modes option adds the UIBackgroundModes key to your app’s Info.plist file. Selecting one or more checkboxes adds the corresponding background mode values to that key.
Now, while you still cannot programmatically access to the Notification Center, you can track the notifications as they come.
An mocked implementation would go as follows:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Hey we got a notification!
// Now we have 30 seconds to do whatever we like...
// ...and then we have to call the completion handler
completionHandler(UIBackgroundFetchResultNoData);
}

Under iOS 7 there is a way to do this.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
And then do whatever you need to do in the app to handle the notification and make sure to register that it was received by the phone.
When a push notification arrives, the system displays the notification to the user and launches the app in the background (if needed) so that it can call this method. Use this method to download any data related to the push notification. When your method is done, call the block in the handler parameter.
Found here.

Related

Doing something even if application is closed in iphone

I am developing iphone application where i want to develop feature of taking backup of files inside the application on to the server for every hour even if the application is completely closed.
I have tried to use NSLocalNotification but it is not calling method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
in appdelegate. The notifier showing alert box with the buttons cancel and view , If I click in view it opens the application but not calling the method. Even if user say cancel then also it should call this method.
So it is not calling web service to take backup of files. Can any one please direct me to link which does the same.
Thanking You,
Rohit Jankar
You really can't do what you want. Since iOS does not allow application to run in the background unless it's is a VOIP client, AudioPlayer or track the user location.
The problem with the solution with UILocalNotification is that iOS handles the notification. When the user clicks cancel your app does not get informed about this. You can only handle the view button clicks.
The-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification is only called if you app is run or is restored from background running.
If you app gets start by the system when you user click view on the Local notification you will need to check if there is a notification in the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method:
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
//handle the notification.
}
I am developing iphone application where i want to develop feature of
taking backup of files inside the application on to the server for
every hour even if the application is completely closed.
If you're talking about backing up files to your own server every few hours, there's really no way to do that. On the other hand, there should also be no need to do it when the app isn't running -- if the app isn't running, it can't modify data in its files and there shouldn't be any need to back up. Instead, just make sure that you back up your data periodically when the app is running.
If you just want to make sure that the app's data is backed up somewhere, consider using iCloud. Once you've set up your app to use iCloud, your data will be backed up whenever you update it.

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.

How do i find out what state the application is in as the result of a notification?

I have a UILocalNotification set up, and as far as i can see it i have 5 different scenarios:
The app is not running, the user chooses to view the notification, so it launches the app.
The app is not running, the user chooses to close the notification, then opens the app at a later date.
The app is running in the background, the user chooses to view the notification, so it brings the app to the foreground.
The app is running in the background, the user chooses to close the notification, then opens the app bringing it to the foreground at a later date.
The app is running in the foreground.
How do i deal with these 5 different scenarios?
Put your code into application:didFinishLaunchingWithOptions:. In
the actions NSDictionary you will find the information about the
notification.
You can again check in application:didFinishLaunchingWithOptions: if the local
notification is still active and take appropriate action.
Put your code into applicationWillEnterForeground:
Again the same spot, just check if there are active local notifications.
Here you can check in application:didReceiveLocalNotification: and either notify the user or not.
Not exactly sure what you're after, but the following might answer your question.
From the documentation:
When the system delivers a local notification, several things can happen, depending on the application state and the type of notification. If the application is not frontmost and visible, the system displays the alert message, badges the application, and plays a sound—whatever is specified in the notification. If the notification is an alert and the user taps the action button (or, if the device is locked, drags open the action slider), the application is launched. In the application:didFinishLaunchingWithOptions: method the application delegate can obtain the UILocalNotification object from the passed-in options dictionary by using the UIApplicationLaunchOptionsLocalNotificationKey key. The delegate can inspect the properties of the notification and, if the notification includes custom data in its userInfo dictionary, it can access that data and process it accordingly. On the other hand, if the local notification only badges the application icon, and the user in response launches the application, the application:didFinishLaunchingWithOptions: method is invoked, but no UILocalNotification object is included in the options dictionary.
If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it. The UILocalNotification instance is passed into this method, and the delegate can check its properties or access any custom data from the userInfo dictionary.

How to handle phone call interrupts in iPhones below iOS 4

In one of my application I need to handle call interrupt. I know application will directly enter background in iOS 4 onwards. How can I handle this situation devices with iOS's less than 4. Do I need to implement any delegate method to be get notified that a call interrupt has come. Please help.
Thanks in advance.. :)
Use the below Appdelegate method:
(void)applicationWillResignActive:(UIApplication *)application
This delegate will call when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or when the user quits the application and it begins the transition to the background state.
To Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. For this use the below appDelegate
(void)applicationDidBecomeActive:(UIApplication *)application
You can have a look at The Application Life Cycle. You will find there the difference between iOS 4.0 and earlier versions.

How to know the app is invoked from background process or not

Thanks in advance.
I used push notification service in my application.If the app is running in background i am able get the alert view, but the app is in active state it is not displaying alert. Is there any way to display alert.
Actually to display alert i am creating an alert in
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo method.
But there is a problam with this alert i.e., of the app is running in background we will get remote alert at the same time we will get this alert also.
So is there any way to find if the app is in active state or in background while receiving remote notification using didReceiveRemoteNotification method.
A simple solution I can think of is to make use of applicationDidBecomeActive: and applicationDidEnterBackground:. Declare a property in your app delegate and set it properly in those two methods. Then you can do anything you want based on this property, like [[[UIApplication sharedApplication] delegate] isInBackground].