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

标签 ios swift notifications

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

尽管我的应用程序中的所有内容都应正确连接(检查通知是否已启用、设置通知类别和委托(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/

相关文章:

ios - Swift Combine-多线程上的Publishers.CombineLatest

ios - 转换为当前 Swift 语法失败 - "No such module"(Swift 4、Xcode 9)

arrays - 在 Swift 中修改字典数组

java - 进度条未在通知中更新

cocoa : Notification if unmount request fails?

ios - 更改图标会导致 SwiftUI 动画出现故障?

ios - 如何使用 Swift 通过单击完成按钮关闭以前和当前的 View Controller

iphone - 禁用通知屏幕 iPhone

ios - SpriteKit 和 Tiles - 处理非正方形的图 block

swift - 使用带有 Swift2 的相机的黑色背景图像