后台swift 4中的自定义本地通知

问题描述 投票:0回答:1

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

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)

在app 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
    }

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

ios swift notifications
1个回答
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 )")

        }
© www.soinside.com 2019 - 2024. All rights reserved.