2017-04-12 80 views
0

我試圖找出如何更新每天重複的本地通知中的消息。如何更新iOS 10中的每日本地通知消息?

目前,我有我的AppDelegate下面的代碼:

func scheduler(at date: Date, numOfNotes: Int) 
{ 
    let calendar = Calendar(identifier: .gregorian) 

    let components = calendar.dateComponents(in: .current, from: date) 

    let newComponents = DateComponents(calendar: calendar, timeZone: .current, hour: components.hour, minute: components.minute) 

    let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: true) 

    content.badge = numOfNotes as NSNumber 

    content.body = "REMINDER: " + String(numOfNotes) + " needs to be looked at!" 

    content.sound = UNNotificationSound.default() 

    let request = UNNotificationRequest(identifier: "reminderNotification", content: content, trigger: trigger) 

    UNUserNotificationCenter.current().delegate = self 

    UNUserNotificationCenter.current().add(request) {(error) in 

    } 
} 

我在UserDefaults存儲numOfNotes。我在我的UITableViewCell一個UISwitch,即在對呼叫進行切換scheduler功能,像這樣:

func remindMeSwitch(_ remindMeSwitch: UISwitch) 
{ 
    numOfNotes = UserDefaults.standard.integer(forKey: "Notes") 

    let delegate = UIApplication.shared.delegate as? AppDelegate 

    delegate?.scheduler(at: time, numOfNotes: numOfNotes) 
} 

然而,設置repeats參數true時每天有通知重複在指定的時間,numOfNotes只調用一次,這是當我切換UISwitch

如何將通知設置爲每日警報但仍能夠根據需要更新通知消息?

謝謝。

+1

那就好像你需要安排適當的個人信息不重複的通知實現這一目標。 – Losiowaty

+0

@Losiowaty,我懷疑是這種情況,但我希望不必每次計數改變時重新計劃 – Pangu

+0

您可以嘗試添加通知服務應用程序擴展,但我不確定是否需要它本地通知。也只有從iOS 10纔可用。 – Losiowaty

回答

0

一般而言,您無法更改本地通知。 只有一種方法 - 刪除/取消舊通知並創建新通知。但是你可以使用複製功能。

例如,如果要更改通知的內容:

// create new content (based on old) 
if let content = notificationRequest.content.mutableCopy() as? UNMutableNotificationContent { 
    // any changes  
    content.title = "your new content's title" 
    // create new notification 
    let request = UNNotificationRequest(identifier: notificationRequest.identifier, content: content, trigger: notificationRequest.trigger) 
    UNUserNotificationCenter.current().add(request) 
}