当应用程序处于后台时,UILocalNotification 未显示在通知中心中
我有一个 GPS
应用程序,它注册为在后台
运行。当我完成一个过程时,我还会显示一个 UILocalNotification
。这可以正确显示,如果应用程序已打开,那么它也会显示在通知中心
中(从顶部向下滑动)。但是,如果我在应用程序处于后台或屏幕锁定时调用 UILocalNotification
,我确实会收到通知,但它不会显示在 通知中心
中。
我在我的应用程序委托中正确注册通知(iOS 5 错误解决方法):
// Register for notifications
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
调用通知:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.alertBody = msg;
localNotif.alertAction = NSLocalizedString(@"View", nil);
localNotif.soundName = @"alert.caf";
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotif];
[localNotif release];
这是错误吗?为什么它仅在我的应用打开时才显示在通知中心
中,即使通知已显示给用户,而不是其他时候?
I have a GPS
app that registers to run in the background
. I also show a UILocalNotification
when I have finished a process. This correctly shows, and if the app is open, then it also appears in Notification Center
(swipe down from top). But, if I call the UILocalNotification
when my app is in the background, or the screen is locked, I DO get the notification, but it does NOT show up in Notification Center
.
I am correctly registering for notifications in my app delegate (iOS 5 bug workaround):
// Register for notifications
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
Calling the notification:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.alertBody = msg;
localNotif.alertAction = NSLocalizedString(@"View", nil);
localNotif.soundName = @"alert.caf";
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotif];
[localNotif release];
Is this a bug? Why would it show up in Notification Center
only when my app is open, even though the notification is shown to the user, and not other times?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您也在使用推送通知吗?我相信如果您只使用本地,则无需调用
registerForRemoteNotificationTypes:
。检查 这里
Are you also using push notifications? I believe there is no need to call
registerForRemoteNotificationTypes:
if you are only using local.Check Here
从您发布的代码来看,该代码看起来不像会在后台运行。看起来您正在尝试在后台发生事件时呈现本地通知。你不能这样做。本地通知旨在在应用程序运行时安排在应用程序未运行时关闭,然后即使应用程序关闭它们也会触发。
您需要使用推送通知。请在此处查看 Apple 的文档。编辑
:您的应用程序是否在设置中启用了通知中心?
From the look of the code you posted, it does not look like the code would run in the background. It looks like you are trying to present a Local Notification when an even occurs in the background. You can not do this. Local Notifications are meant to be scheduled while the app is running to go off at a later time when the app is not running, and then they will trigger even when the app is closed.
You need to use Push Notifications. Check out Apple's documentation here.
EDIT: Is your app enabled for the notification center in settings?
您是否已将 UIBackgroundModes 键添加到您的 Info.plist 文件中?
您需要将 UIBackgroundModes Key 设置为位置。
have you added the UIBackgroundModes key to your Info.plist file ?
You need to have the UIBackgroundModes Key set to location.