2017-04-12 62 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) 
}