本地通知在iOS5上不起作用

时间:2011-11-01 13:02:24

标签: iphone notifications ios5 local

在通知中心配置所有内容后,该应用程序可以显示通知,我的应用程序的本地通知不会触发。

你遇到同样的问题吗?

更多信息:

  1. 几天前使用相同的源代码编译的同一个应用程序,使用XCode 4.1和iOS 4.3 SDK编译,一切正常。

  2. 此外,使用旧版XCode和iOS SDK编译的应用程序可以在升级后在iOS5上运行。

  3. 但是,使用相同代码编译的应用程序,但XCode 4.2和iOS5 SDK不起作用。

    你有什么想法吗? 或者iOS5有什么特别的工作吗?

    示例代码如下:

    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];
    }
    

    谢谢, 迈克尔

1 个答案:

答案 0 :(得分:11)

在iOS 5中,通知由通知中心管理。您必须在通知中心(以编程方式)注册您的申请,或(以非编程方式)转到Settings > Notifications并选择适当的设置,例如启用通知中心,选择提醒方式等。

您可以使用以下代码向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。

相关问题