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

标签 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;

并在应用委托(delegate)文件中接收通知文件
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
   // NSLog(@"%@", notification);
}

请让我知道我在哪里做错了。任何帮助将不胜感激。

最佳答案

你在哪里安排通知?
例如 -

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
本地通知在您安排之前不会出现。

关于ios - 如何在 iOS 中实现本地通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25421451/

相关文章:

ios - 如何将 UITableView 单元格设置为看起来像消息气泡?

ios - 通过硬代码加载与标签栏 Controller 相关的所有 View

ios - 如何在 UITableView 编辑模式下更改左侧图标

Swift 重复本地通知设置

android - Cordova Local Notification Android Plugin 2.2升级

ios - 如何对特定于语言环境的本地通知进行单元测试

iphone - 模态呈现的 TableView 没有导航栏

iphone - 父 NSManagedObjectContext 在子保存后没有变化,但仅在 Release模式下(在 Debug模式下工作)

ios - 每天5个本地通知

Unity中的Android本地通知