简体   繁体   中英

Push notifications with local timezone

I have the following piece of code

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");

And I want to add a local push notification with this data

title: "Test push bericht",
message: "Dit is een test bericht",
datetime: "2014-10-09 09:49:00",
journeytype_id: 13,
language: "nl"

You can see I have 2 timezones in my code. When I run it with the [NSTimeZone defaultTimeZone] I get This log

FIRE DATE 2014-10-09 09:49:00 +0000

Which looks oké but when I print my local notifications I get this

"<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}}"
)

You can see that the time is 2 hours later then the original fire date. However when I do this with the other timezone [NSTimeZone timeZoneWithName:@"GMT"] I get this log for the fire date.

FIRE DATE 2014-10-09 09:49:00 +0000

Which is also ok and when you look at the notification itself:

"<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}}"

You can see that the notification itself is also OK and I get the notification at the time that I want.

PROBLEM

This is a travel app. So sometimes the user is in New York but it can also be in Europe for example. So I know I should work with the [NSTimeZone defaultTimeZone] but this gives me the wrong date/hour.

Can anybody help me how I can fix this? I have always trouble with understanding how NSDates and NStimezones works in Objective-c ...

The usage of debugger on your localNotification.fireDate is misleading because it is expressed in a particular time zone. As a result it will give you the same FIRE DATE log but for a different time zone.

Now when you rely on the log of the UILocalNotification objects you will see some more relevant information. In your scenario the default time zone rely on the CEST time which is GMT + 2h that is why you are seeing a two hours differences in this log.

If i were you i will rely on the localTimeZone instead of the systemTimeZone which is used per default when the defaultTimeZone has not been set. Have a look to the official doc on that matter to have a clear understanding of the different time zones you can use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM