gpt4 book ai didi

ios - 使用本地时区推送通知

转载 作者:行者123 更新时间:2023-11-29 12:36:11 26 4
gpt4 key购买 nike

我有如下一段代码

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] 运行它时,我得到了 This log

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] 但这给了我错误的日期/时间。

谁能帮我解决这个问题?我总是难以理解 NSDatesNStimezonesObjective-c 中的工作方式...

最佳答案

调试器在您的 localNotification.fireDate 上的使用具有误导性,因为它是在特定时区中表示的。因此,它会为您提供相同的 FIRE DATE 日志,但时区不同。

现在,当您依赖于 UILocalNotification 对象的日志时,您会看到一些更相关的信息。在您的场景中,默认时区依赖于 CEST 时间,即 GMT + 2h,这就是您在此日志中看到两个小时差异的原因。

如果我是你,我将依赖 localTimeZone 而不是 systemTimeZone,后者在未设置 defaultTimeZone 时默认使用。去官方看看doc在这个问题上清楚地了解您可以使用的不同时区。

关于ios - 使用本地时区推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26273374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com