iOS Development: Push notifications prevent my local notifications from firing
I have an alarm clock app that's designed to function in a specific way when it's running in the foreground and it receives a local notification that was schedule by my app. When the user sets an alarm, my app creates a local notification. When that local notification fires, it calls the didReceiveLocalNotification
method to alert the user. The problem is, if my app is running in the foreground and push notification comes in from a different app, then it displays the modal push notification view, which prevents my app from alerting the user when the local notification fires. As soon as I dismiss the 开发者_开发技巧push notification modal view, then my app fires the local notification. Is there a way around this? I ask because if my app doesn't set off the alarm when the user schedules it to fire, then it kinda defeats the whole purpose of setting an alarm.
Thanks so much for your wisdom!
There is 5 different states for an application:
- Foreground Active
- Foreground Inactive
- Backgroung Running
- Backgroung Suspended
- Not Running
When you launch your application, it's in "Foreground Active" then, when the modal push notification is displayed your application goes to "Foreground Inactive".
In the documentation, Apple said (http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html):
If it delivers it when the application is not active in the foreground, it displays an alert, badges the application icon, or plays a sound—whatever is specified in the UILocalNotification object. If the application is running in the foreground, there is no alert, badging, or sound; instead, the application:didReceiveLocalNotification: method is called if the delegate implements it.
So in the foreground, application:didReceiveLocalNotification:
gets called only if your app si active, and it become active when you dismiss the modal notification.
This is borderline and there is no solution for this case.
I don't believe there's a way around it. You might be able to play a sound or vibrate, but the OS is already asking for the user's attention with the push notification alert, so it really won't help to post another one until the user deals with the first. How do you handle things when the user schedules two alarms at the same time?
精彩评论