【发布时间】:2014-10-09 08:08:31
【问题描述】:
我有以下代码
NSDictionary *dictionary = [[NSDictionary alloc]initWithObjectsAndKeys:notId,@"id", nil];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
// localNotification.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
localNotification.fireDate = reisPush.rei_datetime;
localNotification.alertBody = reisPush.rei_message;
localNotification.userInfo = dictionary;
NSLog(@"FIRE DATE %@",localNotification.fireDate);
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(@"ADDED NOTIFICATION");
我想用这个数据添加一个本地推送通知
title: "Test push bericht",
message: "Dit is een test bericht",
datetime: "2014-10-09 09:49:00",
journeytype_id: 13,
language: "nl"
您可以看到我的代码中有 2 个时区。当我使用[NSTimeZone defaultTimeZone] 运行它时,我得到了这个日志
FIRE DATE 2014-10-09 09:49:00 +0000
看起来不错,但是当我打印本地通知时,我得到了这个
"<UIConcreteLocalNotification: 0x16d854a0>{fire date = donderdag 9 oktober 2014 11:49:00 Midden-Europese zomertijd, time zone = Europe/Brussels (CEST) offset 7200 (Daylight), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = donderdag 9 oktober 2014 11:49:00 Midden-Europese zomertijd, user info = {\n id = 13;\n}}"
)
您可以看到时间比原始开火日期晚了 2 小时。但是,当我使用其他时区 [NSTimeZone timeZoneWithName:@"GMT"] 执行此操作时,我会收到此日志以了解火灾日期。
FIRE DATE 2014-10-09 09:49:00 +0000
这也可以,当您查看通知本身时:
"<UIConcreteLocalNotification: 0x16e66e50>{fire date = donderdag 9 oktober 2014 09:49:00 GMT, time zone = GMT (GMT) offset 0, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = (null), user info = {\n id = 13;\n}}"
您可以看到通知本身也可以,我在我想要的时间收到通知。
问题
这是一款旅行应用。所以有时用户在纽约,但也可能在欧洲。所以我知道我应该使用[NSTimeZone defaultTimeZone],但这给了我错误的日期/时间。
谁能帮我解决这个问题?
我一直无法理解 NSDates 和 NStimezones 在 Objective-c 中的工作原理...
【问题讨论】:
标签: ios objective-c notifications push-notification uilocalnotification