簡體   English   中英

cancelLocalNotification似乎不起作用

[英]cancelLocalNotification doesn't seem to work

在下面的代碼中,我檢查當前任務(正在編輯的任務)是否具有localNotification。 如果是這樣,我嘗試取消該通知。 然后(如果還沒有通過fireDate)我用(可能)已更改的信息更新通知的alertBody並重新安排它。

問題是,如果我創建一個通知然后進行編輯,最終會關閉兩個通知。 不知何故,原始版本並沒有被取消...

if ([currentTask localNotification])
{
    [[UIApplication sharedApplication] cancelLocalNotification:[currentTask localNotification]];
    if ([[NSDate date] compare:[currentTask localNotification].fireDate] == NSOrderedAscending)
    {
        NSLog(@"current task name: %@", [currentTask name]);
        NSString *temp = [NSString stringWithFormat:@"Alert:\n%@", [currentTask name]];
        [[currentTask localNotification] setAlertBody:temp];
        [[UIApplication sharedApplication] scheduleLocalNotification:[currentTask localNotification]];
    }
    else
        [currentTask setLocalNotification:nil];
}

對這里的問題有什么見解嗎?

我知道在計划通知時會制作一份副本-cancelLocalNotification如何找到正確的副本? 它是否通過測試屬性是否相等來做到這一點? 還是復制指針地址並將其匹配? 換句話說,如果更改了屬性之一,cancelLocalNotification是否無法將其與最初計划的通知進行匹配?

我將在此處為iOS 9添加我的發現。以前,我的代碼如下:

UILocalNotification* notification = getNotification();
[[UIApplication sharedApplication] cancelLocalNotification: notification];
notification.alertBody = newBody;
[[UIApplication sharedApplication] scheduleLocalNotification: notification];

在iOS 9之前,可以很好地工作,取消現有實例,然后使用新的alertBody或soundName重新安排它的時間。 然后是iOS 9,突然運行此代碼實際上並沒有取消先前的通知,因此我最終收到了比我想要的更多的通知!

對我來說,解決辦法是不要懶惰地重用舊通知,因此現在創建一個新通知,並用舊通知的字段填充它。 工作請客!

編輯:我似乎仍然在[[UIApplication sharedApplication] cancelLocalNotification: notification]在其他情況下無法正常工作...我的通知每分鍾自動重復一次,看來Xcode 7.0中可能存在錯誤其有時被忽略: https : //forums.developer.apple.com/thread/9226

編輯II:似乎無法取消針對在針對iOS 9構建之前在應用中安排的重復本地通知..至少不容易...由於我的應用圍繞重復本地通知而仍在努力尋找解決方案提醒!

編輯III:通過嘗試取消應用程序后強制退出該應用程序,我設法擺脫了揮之不去,無法取消的通知……這似乎奏效了……這不是一個很好的解決方案,當然不是什么如果有其他選擇,我想讓我的用戶來做...我已經發布了一個單獨的問題,以查看是否有人有其他想法。

我遇到了同樣的問題。 我使用的解決方案是。

// Sort the notification on the fire date of the notification.
NSSortDescriptor * fireDateDesc = [NSSortDescriptor sortDescriptorWithKey:@"fireDate" ascending:YES];

//get all scheduled notifications.
NSArray * notifications = [[[UIApplication sharedApplication] scheduledLocalNotifications] sortedArrayUsingDescriptors:@[fireDateDesc]];

// Cancel all scheduled notifications.
[[UIApplication sharedApplication] cancelAllLocalNotifications];

for (int i=0; i<[notifications count]; i++){

    UILocalNotification* oneEvent = [notifications objectAtIndex:i];

    // update notification data here an then schedule the notification.

    [[UIApplication sharedApplication] scheduleLocalNotification:oneEvent];

}

逐個取消UILocalNotifications以更新通知中的某些內容會產生一個問題,有時通知不會被取消。

所以我實現的解決方案是:

1:獲取所有計划的通知並將其存儲在您喜歡的任何位置。

2:然后取消所有計划的通知。

3:從第一步循環到通知,更新您想要的任何內容,並在旅途中安排通知。

希望這可以幫助。

似乎自計划以來,UILocalNotification的任何屬性均已更改,cancelLocalNotification將不起作用。 這使我認為cancelLocalNotification必須對照當前計划的通知副本檢查不同屬性的相等性,以查看哪個匹配。

暫無
暫無

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

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