如何 enable/disable 从我的应用推送通知
How to enable/disable push notification from my app
我正在开发具有推送通知的应用程序 属性。我应该 enable/disable 推送通知权限 在 我的应用程序 中 iPhone 设置。
有办法实现吗?
找了很多,没找到合适的实现方式。
有什么帮助吗?
如果用户拒绝推送通知的权限,您不能让他在应用程序中启用它。
但是,您可以在设置应用程序 (ViewController
) 中设置一个按钮,让用户在那里关闭和打开通知。然后您可以设置一个布尔值以在发送通知之前进行检查。这样用户就可以使用它而不是在设备设置上禁用应用程序的通知权限。
启用推送通知(从应用程序设置):
if #available(iOS 10.0, *) {
// SETUP FOR NOTIFICATION FOR iOS >= 10.0
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})
}
}
}else{
// SETUP FOR NOTIFICATION FOR iOS < 10.0
let settings = UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
// This is an asynchronous method to retrieve a Device Token
// Callbacks are in AppDelegate.swift
// Success = didRegisterForRemoteNotificationsWithDeviceToken
// Fail = didFailToRegisterForRemoteNotificationsWithError
UIApplication.shared.registerForRemoteNotifications()
}
委托方法来处理推送通知
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// ...register device token with our Time Entry API server via REST
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
//print("DidFaildRegistration : Device token for push notifications: FAIL -- ")
//print(error.localizedDescription)
}
禁用推送通知:
UIApplication.shared.unregisterForRemoteNotifications()
我正在开发具有推送通知的应用程序 属性。我应该 enable/disable 推送通知权限 在 我的应用程序 中 iPhone 设置。
有办法实现吗?
找了很多,没找到合适的实现方式。
有什么帮助吗?
如果用户拒绝推送通知的权限,您不能让他在应用程序中启用它。
但是,您可以在设置应用程序 (ViewController
) 中设置一个按钮,让用户在那里关闭和打开通知。然后您可以设置一个布尔值以在发送通知之前进行检查。这样用户就可以使用它而不是在设备设置上禁用应用程序的通知权限。
启用推送通知(从应用程序设置):
if #available(iOS 10.0, *) {
// SETUP FOR NOTIFICATION FOR iOS >= 10.0
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})
}
}
}else{
// SETUP FOR NOTIFICATION FOR iOS < 10.0
let settings = UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
// This is an asynchronous method to retrieve a Device Token
// Callbacks are in AppDelegate.swift
// Success = didRegisterForRemoteNotificationsWithDeviceToken
// Fail = didFailToRegisterForRemoteNotificationsWithError
UIApplication.shared.registerForRemoteNotifications()
}
委托方法来处理推送通知
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// ...register device token with our Time Entry API server via REST
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
//print("DidFaildRegistration : Device token for push notifications: FAIL -- ")
//print(error.localizedDescription)
}
禁用推送通知:
UIApplication.shared.unregisterForRemoteNotifications()