gpt4 book ai didi

ios - 后台自定义本地通知 swift 4

转载 作者:行者123 更新时间:2023-11-30 11:10:38 24 4
gpt4 key购买 nike

我已经编写了一些代码来在我的应用程序中实现自定义通知,但当应用程序处于后台模式时,这似乎不起作用,下面是代码:

let content = UNMutableNotificationContent()
content.title = "test notifaction"
content.body = "test notification after 5 second"
content.sound = UNNotificationSound.default()

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: true)
let request = UNNotificationRequest(identifier: "testidentifire", content: content, trigger: trigger)

在应用程序委托(delegate)中

//user notification method
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.sound])
}
//response to user notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.notification.request.identifier == "testidentifire"
{
print("test")
}
completionHandler()
}




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

UNUserNotificationCenter.current().delegate = self

UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge]) { (granted, error) in
print("granted\(granted)")
}
return true
}

现在,当我搜索时,我在每个地方都发现了相同的代码,我无法理解我的代码有什么问题

最佳答案

在你看来试试这个确实加载它对我有用

 //sendign local notification you need three object a contant,trigger,represh
let content = UNMutableNotificationContent()
content.title = "test notifaction"
content.body = "test notification after 5 second"
content.sound = UNNotificationSound.default()

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: true)
let request = UNNotificationRequest(identifier: "testidentifire", content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request) { (error) in
print("error\(error )")

}

关于ios - 后台自定义本地通知 swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52211336/

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