我設置了本地通知。一旦它被解僱,讓我們說我的應用程序處於「睡眠」模式,這是主頁按鈕按下或我退出它。現在收到通知。我在我的應用程序上看到紅色標記,當我點擊該應用程序時,它應該激發didReceiveLocalNotification
,但它不會。當我打開我的應用程序時,我該如何做到這一點?本地通知當應用程序打開時未顯示
0
A
回答
1
didReceiveLocalNotification
方法僅在您的應用程序在前臺運行時調用。如果您看到徽章並單擊應用程序啓動它,則需要使用application:willFinishLaunchingWithOptions:
(或application:didFinishLaunchingWithOptions:
)處理本地通知。要以這兩種方法中的任何一種方式獲取本地通知,請使用UIApplicationLaunchOptionsLocalNotificationKey
作爲選項字典的關鍵字。
[編輯]從UILocalNotification文檔:
如果本地通知僅徽章的應用程序圖標,且 用戶響應啓動應用程序時, 應用中:didFinishLaunchingWithOptions:方法被調用,但沒有 UILocalNotification對象包含在選項字典中。
0
-(void)alarmNotification
{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *newdate = timePicker.date;
NSLog(@"pickerdate:%@",newdate);
NSDate *pickerDate =[newdate dateByAddingTimeInterval:-60*1];
NSLog(@"pickerdate:%@",timePicker.date);
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit)
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = txtMeetingName.text;
// Set the action button
localNotif.alertAction = NSLocalizedString(@"View!", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
localNotif.userInfo = infoDict;
localNotif.applicationIconBadgeNumber =1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
然後實現此代碼......
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
// Handle the notificaton when the app is running
NSDateComponents *comps = [[NSCalendar currentCalendar] components:notif.repeatInterval fromDate:notif.fireDate toDate:[NSDate date] options:0];
// you can set your own sound
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/win.wav", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
UIAlertView *alertView =
[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"move on", nil)
message:[NSString stringWithFormat:@"You have to start %@ meeting within 5 minute",notif.alertBody]
delegate:self
cancelButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"OK", nil), nil];
notif.soundName = UILocalNotificationDefaultSoundName;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog(@"error%@",[error description]);
else
[audioPlayer play];
[alertView show];
[alertView release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex{
if (buttonIndex==0) {
[audioPlayer stop];
}
NSLog(@"U HAVE CLICKED BUTTON");
}
希望這將有助於你.....
+1
我想在didFinishLaunchOptions中獲得它,它與didReceive一起工作良好。實際上,當應用程序關閉並正常打開時,與推送通知不同,它不會在launchOptons中顯示 – 2013-03-20 10:53:49
0
當設置消防日期以後有你設置通知對象的userInfo屬性? &你在設備上還是在模擬器上檢查?
相關問題
- 1. 當應用程序未打開時顯示擡頭通知
- 2. 通知Android:當應用程序打開時不顯示通知?
- 3. Android顯示應用程序未打開時的通知?
- 4. 當應用程序打開時不顯示FCM通知
- 5. 當本地通知點擊Windows Phone 8.1應用程序時,應用程序未打開?
- 6. 本地通知觸發器每次打開應用程序時
- 7. 防止在應用程序打開時顯示通知
- 8. Android - 如何停止顯示通知時打開應用程序
- 9. 當應用程序在後臺顯示通知時不顯示
- 10. 當應用程序打開時,推送通知不起作用
- 11. 本地通知被推送,但應用程序無法打開
- 12. Corona sdk - 如何在本地通知上打開應用程序?
- 13. 打開應用程序時更新應用程序通知
- 14. 應用程序打開或背景(顯示通知與否)
- 15. 當應用程序打開時停止推送通知振動
- 16. 當應用程序退出時顯示遠程通知的uiviewcontroller
- 17. iOS 6本地通知失敗,當手機鎖定應用程序打開
- 18. 本地通知只出現在通知中心,當應用程序在iPhone上打開時
- 19. 當我收到通知時,我的應用程序將在後臺打開,只顯示通知並不打開應用程序?
- 20. 當應用程序打開時未收到Android上的推送通知
- 21. 當應用程序打開時未收到Firebase消息Android通知
- 22. 當主應用程序未運行時顯示通知的後臺進程
- 23. 如何顯示應用程序未運行時的通知
- 24. 當應用程序被殺時設置每日本地通知
- 25. 當應用程序處於後臺時處理本地通知
- 26. Android:當應用程序關閉時顯示通知
- 27. 當應用程序在後臺時不顯示推送通知
- 28. 從通知中打開應用程序
- 29. GCM - 從通知打開應用程序?
- 30. modalViewController當應用程序打開時顯示
啓動選項返回零.....我可以看到通知欄上的通知,但啓動選項返回零當我打開應用程序 – 2013-03-20 02:36:00
launchOptions詞典是否爲零?如果不是,它的內容是什麼? – GoZoner 2013-03-20 03:03:58
是的,這是錯誤的。它是零。它必須返回一些東西。我曾嘗試在didEnterBackground中放置一個簡單的通知。打開的應用程序,按下主頁按鈕。使用Xcode關閉應用程序,然後獲得通知後,使用Xcode進行反駁。還沒有。 – 2013-03-20 03:10:21