且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在模拟器中的iphone本地通知

更新时间:2023-02-26 21:09:50

是的,本地通知适用于模拟器。但是,如果您希望在应用程序位于前台时看到nofication,请确保在应用委托中实现应用程序:didreceiveLocalNotification:

Yes, local notifications work with the simulator. However, make sure you are implementing application:didreceiveLocalNotification in your app delegate if you want to see the nofication while your app is in the foreground:

- (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示例工作:

Otherwise, make sure you schedule your notification for some time in the future, then close the application, in order to see the Apple sample work:

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];

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

It's easy to think you're not implementing the test code correctly, and you just aren't handling the event while the app is running.