1

我有下面這段代碼推送通知當地時區

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],但這給我錯誤的日期/小時。

任何人都可以幫我解決這個問題嗎? 我總是理解NSDatesNStimezones作品Objective-c ...

回答

1

調試器對您的localNotification.fireDate的使用如何被誤導,因爲它是在一個特定的時區表示的麻煩。因此,它會爲您提供相同的FIRE DATE日誌但不同時區。

現在,當您依賴UILocalNotification對象的日誌時,您將看到更多相關信息。在您的情況下,默認時區依賴於GMT + 2h的CEST時間,這就是爲什麼您在此日誌中看到兩小時差異的原因。

如果我是你,我將依賴0​​而不是systemTimeZone,默認情況下使用defaultTimeZone時未使用。看看這個問題上的官方doc,以清楚地瞭解您可以使用的不同時區。

+0

我該如何解決這個問題? – Steaphann 2014-10-09 08:51:27

+0

當您收到您的通知時,您需要考慮當地通知的時區。 – tiguero 2014-10-09 08:53:50

+0

我不太明白。例如,用戶在紐約想要在15:00進行本地通知。我只是將fireDate設置爲15:00,不是嗎? – Steaphann 2014-10-09 08:57:49