UILocalNotification without alert - iphone

Is it possible to to call didReceiveLocalNotification without displaying Alert when in background?

didReceiveLocalNotification will only be called if you app is in the foreground or when the user clicked to view the notification and you app is restored to the foreground from the background.
You might be able to use a UILocalNotification with out a message, by example update your application icon badge. But then the didReceiveLocalNotification will not be called if you apps in not the in foreground.

No there is no way sadly, UILocalNotication has been designed to notify the user with an alert view, thats how apple intended them to work
You could set notification.alertBody to nil, but then you will not be able to use this local notification when the application is not active

Related

Snooze Alarm in Alarm Clock Application in iPhone?

I've been working on Alarm Clock application and using local notifications to pop the alarm.
I also need to snooze the alarm as per the time interval set by the user in user settings.
The user cannot snooze until the application starts i.e below mentioned method is called.
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
As I'm calling the method to snooze alarm by adding the time interval in firedate and rescheduling the local notification.
The problem is the application comes to foreground and according to Apple's HIG I should not terminate the app or send the app to background automatically and hence user needs to press home button to send the app in background.
Recently, I came across this app. It can snooze the alarm the way I want to.
One more issue is I can select sound to be played from My Music Library in this app.
Any idea how this app manages to do so?
In order to do what you are after there are several things to consider. Your approach of using UILocalNotification is correct so that is a good start.
In order implement a 'snooze' feature properly you will need to consider the app running in both the foreground and the background.
If the app is in the foreground (or the user opens the app from the notification banner from outside) application:didReceieveLocalNotification: will be called and you can simply update your UI to show a snooze button and schedule a new local notification from the duration loaded from NSUserDefaults.
If the app is the in background and the user closes the notification then there is no way to subsequently try and 'snooze' the alarm.
As for playing a custom sound from the iTunes library take a look at the soundName property on UILocalNotification

iOS UILocalNotification not updating app icon

first of all, I'm really thankful for all your help.
I need just one more little help.
I finished my app, and I used UILocalNotifications to fire some reminders. The app icon is always with a "1" saying there is 1 new notification on the app but it is like that for ever, even if there is no notification. How can I fix this?
Other detail, when the user slides the app icon when iPhone fires the notification (outside the app when the iPhone is locked or when the app is in the background), I would like the app to load in the main view. Is it possible?
You can reset the badge number inside you application delegate code, in didFinishLaunchingWithOptions and didReceiveLocalNotification: as appropriate. i.e.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
I don't understand your second question.
If you want to change the view it displays depending upon how its launched then you can do so, once again this would be in didFinishLaunchingWithOptions or didReceiveLocalNotification.

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.

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.

Is there a way to make my background iPhone app to go foreground?

Is there a way to make my background iPhone app to go foreground?
Tnx.
A UILocalNotification will bring the app to the foreground if the device is locked, a notification appears, and the user unlocks the device.
A UILocalNotification with an alertAction will display the alert while the device is unlocked, and if the user taps the View button (or whatever you set it to), your app will be brought to the front.
Not for your app, but the user could do it.
You could schedule a UILocalNotification to inform the user that you are done with your task or whatever.
Nope. But you can do something when you feel must do. For example, use remote Apple Push Notification Service (APNS) to notify your user that you want her to bring your app back to the foreground.
Or, as JustSid put it, use local notification to notify your user that you want her attention.