【发布时间】:2011-04-05 01:48:44
【问题描述】:
我刚刚下载了 xcode 并尝试制作本地通知示例。问题是本地通知是否适用于模拟器?
谢谢
【问题讨论】:
标签: iphone notifications local
我刚刚下载了 xcode 并尝试制作本地通知示例。问题是本地通知是否适用于模拟器?
谢谢
【问题讨论】:
标签: iphone notifications local
是的,本地通知适用于模拟器。但是,如果您想在应用处于前台时看到通知,请确保在应用委托中实现 application:didreceiveLocalNotification:
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"MyAlertView"
message:notification.alertBody
delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
if (alertView) {
[alertView release];
}
}
否则,请确保将通知安排在未来某个时间,然后关闭应用程序,以便查看 Apple 示例工作:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = [[NSDate date] addTimeInterval:10]; // adds 10 secs
localNotif.fireDate = fireTime;
localNotif.alertBody = @"Alert!";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
很容易认为您没有正确实现测试代码,只是在应用运行时没有处理事件。
【讨论】:
对于偶然发现这个老问题的任何人,您可能会发现另一个问题:iOS 8 引入了新的通知权限;并且您的应用程序必须明确要求它们。
在你的AppDeligate.m:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//register local notifications
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
//the rest of your normal code
return YES;
}
如果您不这样做,您的通知将永远不会触发,并且您会在日志中收到类似这样的精彩消息:
"Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7ae51b10>{... alert details ...} with an alert but haven't received permission from the user to display alerts"
【讨论】:
本地通知在模拟器上工作,推送通知不
【讨论】:
要在 iPhone 模拟器中测试本地通知,请按以下步骤操作:
这些步骤帮助我始终获得成功的本地通知。
【讨论】: