Local Notification doesn't work on iOS5

a 夏天 提交于 2019-11-28 01:33:05

问题


After configuring everything in notification center, which allows the app to display the notification, my app's local notification doesn't fire.

Do you encounter the same problem?

more information:

  1. The same app compiled from the same source code a few days ago, which compiled with XCode 4.1 and iOS 4.3 SDK, everything works well.

  2. In addition, the app compiled with old version XCode and iOS SDK, can work on iOS5, after upgrade.

However, the app which compiled with the same code, but XCode 4.2 and iOS5 SDK doesn't work.

Do you have any ideas? Or is there any special work for iOS5?

The sample code is like:

UIApplication *app = [UIApplication sharedApplication];
NSArray *oldNotifications = [app scheduledLocalNotifications];

// Clear out the old notification before scheduling a new one.
if (0 < [oldNotifications count]) {

    [app cancelAllLocalNotifications];
} 

// Create a new notification
UILocalNotification *alarm = [[UILocalNotification alloc] init];
if (alarm) {

    alarm.fireDate = theDate;
    alarm.timeZone = [NSTimeZone defaultTimeZone];
    alarm.repeatInterval = NSDayCalendarUnit; //repeat every day
    alarm.alertBody = [NSString stringWithFormat:@"alert"];     
    [app scheduleLocalNotification:alarm];
    [alarm release];
}

Thanks, Michael


回答1:


In iOS 5, notifications are managed by Notification Center. You have to register your application with the Notification Center (programmatically), or (non-programmatically) go to Settings > Notifications and select appropriate settings i.e. enable Notification Center, select Alert Style, and others.

You can use following piece of code to register your application with Notification Center (programmatically), by putting it in applicationDidFinishLaunching::

// Although Register For Remote Notifications is not required for Local Notifications,
// but in iOS 5's Notifications, we have to register otherwise the system doesn't register/recognize
// the notifications posted from the application. Note that this behavior is not documented
// as of Oct 2011, and it's possible that it's a bug and will be handled in the future releases.

[[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
 UIRemoteNotificationTypeAlert |
 UIRemoteNotificationTypeSound];

HTH.



来源:https://stackoverflow.com/questions/7966856/local-notification-doesnt-work-on-ios5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!