我已经从日期选择器中设置了一个关于火灾日期/时间的通知。当这种情况发生时,我想运行一种方法,在通知时有方法这样做吗?
这是我的密码
-(void)scheduleLocalNotificationWithDate:(NSDate *)fireDate{
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = fireDate;
notification.alertBody = @"time hit";
notification.soundName = @"alarmSoundFile.mp3";
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
}发布于 2014-09-09 19:24:04
首先,您应该设置一些信息来标识这组通知(以防将来使用其他本地通知)。另外,例如,您可以将自1970年以来的时间间隔设置为ID:
notification.userInfo = @{@"group" : @"time hit",
@"id" : @(fireDate.timeIntervalSince1970)};然后在appDelegate中接收通知:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
[self application:application didReceiveLocalNotification:localNotification];
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSDictionary *userInfo = notification.userInfo;
if ([(NSString *)userInfo[@"group"] isEqualToString:@"time hit"]) {
// some actions here
}
}注意:据我所知,只有当申请程序在火灾日期时才能收到通知,或者如果您将启动/输入带有发射通知的前台,则您才能收到通知。
https://stackoverflow.com/questions/25750799
复制相似问题