本地通知在 iOS5 上不起作用 [英] Local Notification doesn't work on iOS5

查看:32
本文介绍了本地通知在 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?

更多信息:

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

  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和iOS SDK编译的应用程序,升级后可以在iOS5上运行.

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

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

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

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

Do you have any ideas? Or is there any special work for 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];
}

谢谢,迈克尔

推荐答案

在 iOS 5 中,通知由通知中心管理.您必须(以编程方式)向通知中心注册您的应用程序,或者(以非编程方式)转到 Settings >通知并选择适当的设置,即启用通知中心、选择警报样式等.

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.

您可以使用以下代码向通知中心注册您的应用程序(以编程方式),方法是将其放入 applicationDidFinishLaunching::

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.

这篇关于本地通知在 iOS5 上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆