在模拟器中的iphone本地通知

use*_*302 37 iphone notifications local

我刚刚下载了xcode并尝试制作本地通知示例.问题是本地通知是否在模拟器中有效?

谢谢

boj*_*ais 67

是的,本地通知适用于模拟器.但是,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];
    }
}
Run Code Online (Sandbox Code Playgroud)

否则,请确保将来安排通知一段时间,然后关闭应用程序,以便查看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];
Run Code Online (Sandbox Code Playgroud)

很容易认为您没有正确实现测试代码,而您只是在应用程序运行时不处理该事件.


mix*_*x3d 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;
}
Run Code Online (Sandbox Code Playgroud)

如果你不这样做,你的通知将永远不会被激活,你会在你的日志中得到这样的精彩信息:" Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7ae51b10>{... alert details ...} with an alert but haven't received permission from the user to display alerts"


Aar*_*ers 7

本地通知在模拟器上工作,推送通知不工作