gpt4 book ai didi

ios - "Remind me later"本地通知功能

转载 作者:可可西里 更新时间:2023-11-01 01:21:47 28 4
gpt4 key购买 nike

我想通过添加“稍后提醒我”操作来扩展我的本地通知功能。换句话说,如果用户点击“稍后提醒我”按钮,我想在设定的时间后重新显示通知。

即使我的应用程序中的所有内容都应该正确连接(检查通知是否启用、设置通知类别和委托(delegate)、处理稍后提醒我的功能),但在点击稍后提醒按钮后安排的通知不会显示在全部。

设置一切(检查权限,设置类别)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {(accepted, error) in
if !accepted {
print("Notification access denied.")
}
}

let action = UNNotificationAction(identifier: "remindLater", title: "Remind me later", options: [])
let category = UNNotificationCategory(identifier: "myCategory", actions: [action], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])

return true
}

处理点击“稍后提醒我”

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

if response.actionIdentifier == "remindLater" {
let newDate = Date(timeInterval: 10, since: Date())
scheduleNotification(at: newDate, withCompletionHandler: {
completionHandler()
})
}
}

设置本地通知的实际代码:

func scheduleNotification(at date: Date) {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)

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

let content = UNMutableNotificationContent()
content.title = "Tutorial Reminder"
content.body = "Just a reminder to read your tutorial over at appcoda.com!"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "myCategory"

let request = UNNotificationRequest(identifier: date.description, content: content, trigger: trigger)

UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}

我做错了什么? I am using AppCoda iOS 10 user notifications guide.this is the sample code .

最佳答案

问题是您需要为您安排的每个通知使用唯一标识符。您可以使用触发日期描述。不要忘记您需要关闭应用程序才能收到通知。

关于ios - "Remind me later"本地通知功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43524031/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com