1
可能重複:
UILocalNotification isn't working at alliPhone本地通知將不會出現
我正在寫通過通知中心向用戶發送警報,當一個事件日期日益臨近的應用。但是,當我在日期選擇器中設置日期並關閉應用程序時,通知不會顯示。我已在我的配置文件中啓用了推送通知。這一切在我的項目,與通知中心打交道的代碼,這是所有的代碼在處理日期選取器我的視圖控制器文件:
- (IBAction)dateChanged:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *selectedDate = [self.datePicker date];
[defaults setObject:selectedDate forKey:@"ImportantDatesViewController.selectedDate"];
[defaults synchronize];
}
- (void)viewDidLoad {
NSDate *storedDate = [[NSUserDefaults standardUserDefaults]
objectForKey:@"ImportantDatesViewController.selectedDate"];
if (storedDate == nil) {
storedDate = [NSDate date];
}
[self.datePicker setDate:storedDate animated:NO];
}
而這一切都在我的應用程序委託處理本地通知:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
....
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm'/'dd'/'yyyy"];
NSDate *eventDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"ImportantDatesViewController.selectedDate"];
localNotif.fireDate = [eventDate dateByAddingTimeInterval:-13*60*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Event in three days!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
return YES;
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString* pushToken = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
NSLog(@"%@", pushToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
NSLog(@"error: %@", error);
}
任何幫助非常感謝,謝謝!
恐怕我可能在另一個問題上感到困惑,當我從'NSUserDefaults'中檢索時使用鍵的名稱。這是個人喜好的問題,以確保密鑰是唯一的。事情的真相是,你應該使用你用來存儲它的任何東西來檢索它。找到你正在存儲'eventDate'的地方並使用相同的密鑰。 –
非常感謝Mark。我試圖確定我在哪裏存儲eventDate,但我遇到了一些問題。它可能在我的筆尖採摘器中嗎? – John
在哪個視圖控制器中,您要求用戶輸入此日期是否要觸發本地通知?在那個類中,在某個地方,你將一個日期存儲到'NSUserDefaults'中。使用相同的密鑰。現在你明白了爲什麼我提到將鍵存儲爲常量。 :) –