2013-03-20 121 views
0

我設置了本地通知。一旦它被解僱,讓我們說我的應用程序處於「睡眠」模式,這是主頁按鈕按下或我退出它。現在收到通知。我在我的應用程序上看到紅色標記,當我點擊該應用程序時,它應該激發didReceiveLocalNotification,但它不會。當我打開我的應用程序時,我該如何做到這一點?本地通知當應用程序打開時未顯示

回答

1

didReceiveLocalNotification方法僅在您的應用程序在前臺運行時調用。如果您看到徽章並單擊應用程序啓動它,則需要使用application:willFinishLaunchingWithOptions:(或application:didFinishLaunchingWithOptions:)處理本地通知。要以這兩種方法中的任何一種方式獲取本地通知,請使用UIApplicationLaunchOptionsLocalNotificationKey作爲選項字典的關鍵字。

[編輯]從UILocalNotification文檔:

如果本地通知僅徽章的應用程序圖標,且 用戶響應啓動應用程序時, 應用中:didFinishLaunchingWithOptions:方法被調用,但沒有 UILocalNotification對象包含在選項字典中。

+0

啓動選項返回零.....我可以看到通知欄上的通知,但啓動選項返回零當我打開應用程序 – 2013-03-20 02:36:00

+0

launchOptions詞典是否爲零?如果不是,它的內容是什麼? – GoZoner 2013-03-20 03:03:58

+0

是的,這是錯誤的。它是零。它必須返回一些東西。我曾嘗試在didEnterBackground中放置一個簡單的通知。打開的應用程序,按下主頁按鈕。使用Xcode關閉應用程序,然後獲得通知後,使用Xcode進行反駁。還沒有。 – 2013-03-20 03:10:21

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屬性? &你在設備上還是在模擬器上檢查?

+0

通知已啓動只需很好。當我點擊通知時,它確實會調用didReceiveNotification。然而,與應用程序關閉時的推送通知不同,它不會顯示在didFinishWithLaunchOptions的啓動程序選項中。 – 2013-03-20 10:52:49

+0

Ok ..但是您是否設置了通知對象的屬性userinfo? – Kalyani 2013-03-20 11:00:31

+0

@MuhammadUmar&r在模擬器上或在真實設備上測試 – Kalyani 2013-03-20 11:02:01

相關問題