Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 本地通知未与NSDate字符串链接_Ios_Objective C_Nsdate_Uialertview_Uialertviewdelegate - Fatal编程技术网

Ios 本地通知未与NSDate字符串链接

Ios 本地通知未与NSDate字符串链接,ios,objective-c,nsdate,uialertview,uialertviewdelegate,Ios,Objective C,Nsdate,Uialertview,Uialertviewdelegate,我的问题是关于我的申请通知 我正在为我的音乐应用程序使用提醒通知,因此人们可以在我播放节目前两小时收到通知。我之前测试过类似的代码,但是使用了整数组件,而不是NSString日期,并且成功了。但是,我需要在这个特定的实例中使用字符串 对我来说,一切看起来都应该正常运转。位于singleton中的NSLog输出正确的信息。例如: NSLog(@"Notification will be shown on: 2014-07-18 11:03:00 +0000) 那为什么我没有收到通知呢?我试了一段

我的问题是关于我的申请通知

我正在为我的音乐应用程序使用提醒通知,因此人们可以在我播放节目前两小时收到通知。我之前测试过类似的代码,但是使用了整数组件,而不是NSString日期,并且成功了。但是,我需要在这个特定的实例中使用字符串

对我来说,一切看起来都应该正常运转。位于singleton中的NSLog输出正确的信息。例如:

NSLog(@"Notification will be shown on: 2014-07-18 11:03:00 +0000)
那为什么我没有收到通知呢?我试了一段时间,一整天都遇到了路障。希望能得到一些帮助

谢谢大家!

- (void)scheduleNotificationForDate:(NSDate *)date {

    // Here we cancel all previously scheduled notifications
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

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

    localNotification.fireDate = date;
    NSLog(@"Notification will be shown on: %@",localNotification.fireDate);

    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.alertBody = [NSString stringWithFormat:@"Aurora Radio plays in 2 hours!"];
    localNotification.alertAction = NSLocalizedString(@"View details", nil);

    /* Here we set notification sound and badge on the app's icon "-1"
       means that number indicator on the badge will be decreased by one
       - so there will be no badge on the icon 
    */
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.applicationIconBadgeNumber = 1;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
IBAction=

-(IBAction)setReminder {

    // Setting all the information about our date
    NSString * dateString = @"2014-07-18 11:03:00";

    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSDate *myNewDate = [dateFormatter dateFromString:dateString];

    [self scheduleNotificationForDate:myNewDate];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Aurora Radio"
                                                message:@"Alarm has been set two hours prior to performance!"
                                               delegate:self
                                      cancelButtonTitle:@"Sounds good bros, see you later."
                                      otherButtonTitles:nil];
    [alert show];
}

您是否已验证您的字符串是否正在转换为正确的NSDate值?可能您的应用程序不在通知中心和/或您的应用程序已禁用通知?HotLicks-是。NSLog没有验证这一点吗@霍利克斯-不应该这样。我以前使用整数组件对它进行了测试,并得到了一个结果。除此之外,在这种情况下,我需要使用字符串而不是单个整数。@Machina,因此您居住在离所选时区5小时远的地方–因此,难怪通知永远不会被触发,因为它已经过期5小时了,因为您弄乱了时区仅供参考:2014-07-18 16:03:00+0000的日期与2014-07-18 11:03:00-0500的日期完全相同,并且NSLog始终在控制台上显示+0000时区中的日期,因此立即删除该行:[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0];享受你的应用程序终于正常工作了。@holex是对的,我打赌问题在于时区。你不能假设用户在你的时区。你现在在哪个时区?不要忘记,无论您做什么,NSDate都将始终与UTC一起工作,并将在其字符串表示形式中反映这一事实。