简体   繁体   English

本地通知在iOS5上不起作用

[英]Local Notification doesn't work on iOS5

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. 几天前使用相同的源代码编译的相同应用程序,使用XCode 4.1和iOS 4.3 SDK编译,一切运行良好。

  2. In addition, the app compiled with old version XCode and iOS SDK, can work on iOS5, after upgrade. 此外,使用旧版XCode和iOS SDK编译的应用程序可以在升级后在iOS5上运行。

However, the app which compiled with the same code, but XCode 4.2 and iOS5 SDK doesn't work. 但是,使用相同代码编译的应用程序,但XCode 4.2和iOS5 SDK不起作用。

Do you have any ideas? 你有什么想法? Or is there any special work for iOS5? 或者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 谢谢,迈克尔

In iOS 5, notifications are managed by Notification Center. 在iOS 5中,通知由通知中心管理。 You have to register your application with the Notification Center (programmatically), or (non-programmatically) go to Settings > Notifications and select appropriate settings ie enable Notification Center, select Alert Style, and others. 您必须使用通知中心(以编程方式)注册您的应用程序,或(非编程)转到Settings > Notifications并选择适当的设置,例如启用通知中心,选择警报样式等。

You can use following piece of code to register your application with Notification Center (programmatically), by putting it in applicationDidFinishLaunching: : 您可以使用以下代码通过Notification Center(以编程方式)向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. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM