我已经编写了一些代码以在我的应用中实现自定义通知,但是当应用处于后台模式时,这似乎不起作用,以下代码如下:
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)
在应用程序委托中
//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
}
现在,当我进行搜索时,在我无法理解自己的代码出什么毛病的地方都找到了相同的代码
答案 0 :(得分:0)
在您的视图中尝试此操作确实为我加载了
//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 )")
}