PhoneGap iOS LocalNotifications Daylight Savings

℡╲_俬逩灬. 提交于 2019-12-08 01:40:47

问题


I'm using (the infamous) Drew Dahlman's Phonegap Local Notifications in iOS. And it seems to work.

I have never written a line of objective-c and would prefer not to (which is probably why I'm having trouble now!) but I think the plugin is not working quite right and would like to fix it if possible.

When I set a notification for today at 13:00 it fires correctly. However when daylight savings is in play the notification fires at 14:00, an hour late.

Is this because timezones are not being taken into account? Drew mentions that the timezone being set to +0000 is not a problem, but is this mis-information? Should I update the plugin to handle timezones and daylight savings? Or handle daylight saving in my own code and simply set the date -1 hour when DST is active?

I think I have to update the second of these lines in LocalNotification.m but as I said I am unsure of what to update it to.

UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.timeZone = [NSTimeZone defaultTimeZone];

回答1:


Ah there seems to be a bit of a strange issue when notifications are set.

Example 1: When I do something like this, where duedate is a value of 2013-04-08T21:30

var d = new Date(duedate).getTime();
d = new Date(d);

plugins.localNotification.add({
date: d,
...

The notification is logged as being set for 21:30, like so:

Notification Set: 2013-04-08 21:30:00 +0000

Example 2: if I set the duedate like this: (where the time now is 21:30)

var d = new Date().getTime();

The notification is logged as being set for 20:30, like so:

Notification Set: 2013-04-08 20:30:00 +0000

However in the second example the notification fires at 21:30, due to day light savings.

Solution (of sorts)

I'm going to have to add or subtract the difference in time between now and when I want the notification to fire, rather than set the date specifically.




回答2:


This is rather interesting. One of the biggest things I deal with while creating the plugin was the weird way that JS and OBJ-C interpret time. According to docs you can use the systemTimeZone but that is again set by the system which is what the plugin is relying on. That is the time that the user has set and the phone is using.

Even thought it defaults to the +000GMT it still accounts for this in the system time and makes the adjustments for +0500 or what ever zone you're in.

UILocalNotification not using the correct timezone

NSTimeZone: what is the difference between localTimeZone and systemTimeZone?

Both deal with this - the plugin by default is using the system time.



来源:https://stackoverflow.com/questions/15881445/phonegap-ios-localnotifications-daylight-savings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!