我刚刚下载了xcode并尝试制作本地通知示例。问题是本地通知是否在模拟器中有效?
谢谢
答案 0 :(得分:65)
是的,本地通知适用于模拟器。但是,如果您希望在应用处于前台时看到通知,请确保在应用委托中实施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];
很容易认为您没有正确实现测试代码,并且您只是在应用程序运行时没有处理该事件。
答案 1 :(得分:20)
对于任何偶然发现这个旧问题的人来说,你可能会发现另一个问题: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;
}
如果您不知道,您的通知将永远不会触发,您将在日志中收到如此精彩的消息:
&#34; Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7ae51b10>{... alert details ...} with an alert but haven't received permission from the user to display alerts
&#34;
答案 2 :(得分:7)
本地通知适用于模拟器,推送通知不适用
答案 3 :(得分:1)
是本地通知处理本地通知。苹果文档Click here。
答案 4 :(得分:0)
要在iphone模拟器中测试本地通知,请按以下步骤操作:
这些步骤帮助我始终获得成功的本地通知。