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

Push notification of mac OSX when is force quit

提问人:Siddhant Mishra 提问时间:7/23/2023 更新时间:7/25/2023 访问量:38

问:

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

Swift Objective-C macOS Cocoa Apple-推送通知

评论


答:

0赞 Madushan 7/25/2023 #1

是的,你可以,首先你必须确认 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)

}

评论

0赞 Siddhant Mishra 7/27/2023
如果 mac 应用未运行,则不会调用 didReceiveRemoteNotification