簡體   English   中英

本地通知未在 iphone 中觸發

[英]Local notification not firing in iphone

在我的我正在使用操作表選擇器視圖。 它工作正常。 但是我想在time picker中設置特定時間時觸發本地local notification ,它不會觸發local notification 我使用了這段代碼:

enter code here

      - (void)actionPickerDone {

       if (nil != self.data) {
//send data picker message[self.target performSelector:self.action     withObject:[NSNumbernumberWithInt:self.selectedIndex]];
        }
else {
    //send date picker message
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    // Get the current date
    NSDate *pickerDate = [self.datePicker 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];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = itemDate;
            NSLog(@"%@what is this",itemDate);
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
            NSLog(@"i8i8i88i");

    // Notification details
    //localNotif.alertBody = [eventText text];
    // Set the action button
    localNotif.alertAction = @"View";

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;

    // Specify custom data for the notification
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
    localNotif.userInfo = infoDict;

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];
    }

這段代碼有什么問題嗎?請幫助我。

我執行了您的代碼,但通知沒有觸發。 然后我取消了 localNotif.alertBody 賦值語句的注釋,它確實觸發了。 如果您未指定 alertBody,本地通知將不會顯示為警報。 加上 Idan 提出的觀點也是有效的。 如果您的應用程序在前台,則不會顯示通知,而是您將在應用程序委托中獲得回調,您應該在其中顯示帶有消息 notification.alertBody 的警報以保持一致性。

希望這可以幫助:)

PS你不需要你正在做的所有壓延機組件。 記錄它們,你會發現pickerDate 和itemDate 是一樣的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM