模拟器中的iphone本地通知
我刚刚下载了xcode,并尝试制作本地通知示例。问题是本地通知是否在模拟器中起作用模拟器中的iphone本地通知,iphone,notifications,local,Iphone,Notifications,Local,我刚刚下载了xcode,并尝试制作本地通知示例。问题是本地通知是否在模拟器中起作用 谢谢本地通知在模拟器上工作,推送通知不工作是的,本地通知在模拟器上工作。但是,如果希望在应用程序处于前台时看到通知,请确保在应用程序代理中实现了application:didreceiveLocalNotification: - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotifi
谢谢本地通知在模拟器上工作,推送通知不工作是的,本地通知在模拟器上工作。但是,如果希望在应用程序处于前台时看到通知,请确保在应用程序代理中实现了
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];
很容易认为您没有正确实现测试代码,只是在应用程序运行时没有处理事件。要在iPhone模拟器中测试本地通知,请执行以下步骤:
这些步骤帮助我始终获得成功的本地通知。对于任何遇到这个老问题的人来说,你可能会发现另一个问题: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;
}
如果您不这样做,您的通知将永远不会发出,您将在日志中收到如下精彩消息:
“正在尝试安排带有警报的本地通知{…警报详细信息…},但尚未收到用户显示警报的权限
”