如何初始化本地通知?

时间:2011-07-21 12:45:13

标签: iphone

我想在我的时钟应用程序中实现本地通知。基本上我希望每半个小时后播放一个音乐文件,就像每30分钟播放一次钟声的船钟一样。

任何人都可以粗略地了解即使应用程序进入后台也能实现此功能吗?

2 个答案:

答案 0 :(得分:1)

我最近使用了本地通知内容并使用了以下功能

//设置本地通知

for (int i= 1 ; i<=10; i++) { //We here set 10 Notification after every 30 minutes from now you can modify it accordingly

    NSDate *scheduled = [[NSDate date] dateByAddingTimeInterval:60*30*i]; //These are seconds

    NSDictionary* dataDict = [NSDictionary dictionaryWithObjectsAndKeys:scheduled,FIRE_TIME_KEY,@"Background Notification received",NOTIFICATION_MESSAGE_KEY,nil];

    [self scheduleNotificationWithItem:dataDict];

}

其中scheduleNotificationWithItem定义为

- (void)scheduleNotificationWithItem:(NSDictionary*)item  {

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    if (localNotification == nil)   return;

    localNotification.fireDate =  [item valueForKey:FIRE_TIME_KEY];

    localNotification.timeZone = [NSTimeZone defaultTimeZone];

    localNotification.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@", nil), [item valueForKey:NOTIFICATION_MESSAGE_KEY]];

    localNotification.alertAction = NSLocalizedString(@"View Details", nil);

    localNotification.soundName = UILocalNotificationDefaultSoundName;

    localNotification.userInfo = item;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

    [localNotification release];
}

最后,您可以将这些通知视为

您可以按照以下方式处理这些通知

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

// Do the required work you can obtain additional Info via notification.userInfo which happens to be a dictionary

}

阅读开发人员文档将有助于您更好地理解这些内容。希望它有所帮助

答案 1 :(得分:0)

您可以根据您的要求使用UILocalNotifications并设置其“炒作”,然后安排通知。这些通知不会打扰您的应用是否正在运行或处于后台,它们将始终显示为警报视图。