swift 强制退出时的mac OSX推送通知

rjzwgtxy  于 2023-08-02  发布在  Swift
关注(0)|答案(1)|浏览(105)

当收到推送通知但mac应用程序被强制杀死时,是否可以获得回调?

eufgjt7s

eufgjt7s1#

是的,您可以,首先您必须确认AppDelegate中的UNUserNotificationCenter.current().delegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    //here you have to put these line of code to confirm relevant delegate and to enable device for remote notification if device is still not given access for it
    UIApplication.shared.registerForRemoteNotifications()
    UNUserNotificationCenter.current().delegate = self
    
    return true
}

字符串
之后,你必须添加以下两个单独的方法来处理推送通知。
如果您设备iOS版本低于10.0,请使用此方法..

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    //To print notification payload
    print(userInfo)

}


否则您可以使用此方法处理推送通知。

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

//To print notification payload:
print(response.notification.request.content.userInfo)

}

相关问题