如何在iOS中实现本地通知?

时间:2014-08-21 08:18:21

标签: ios localnotification

我正在尝试在特定时间实施本地通知呼叫。这是我的代码。在我做错的地方,请帮帮我  在.m文件中

NSDateComponents* deltaComps = [[NSDateComponents alloc] init] ;
         [deltaComps setDay:1];
         NSDate* tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:deltaComps toDate:[NSDate date] options:0];


        //
      //  NSDate *date = [NSDate date];
       NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
       // NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit)
       //                                                fromDate:tomorrow];

        NSDateComponents *temp = [[NSDateComponents alloc]init];

        [temp setYear:2014];
        [temp setMonth:8];
        [temp setDay:21];
        [temp setHour: 13];
        [temp setMinute:38];

        NSDate *fireTime = [calendar dateFromComponents:temp]; // December 31st 2013 9:00


        // set up the notifier
        UILocalNotification *localNotification = [[UILocalNotification alloc]init];

        localNotification.fireDate = fireTime;
        localNotification.timeZone = [NSTimeZone systemTimeZone];
        localNotification.alertBody=@"Nishant";
        localNotification.alertAction = NSLocalizedString(@"Show", nil);
        localNotification.soundName = @"Gentle Rise.m4a";
        localNotification.hasAction = YES;

并在app委托文件中接收通知文件

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
   // NSLog(@"%@", notification);
}

请告诉我我在哪里做错了。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

你在哪里安排通知?

例如 -

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

localNotification将不会出现,直到您安排它。