在应用内启用或禁用Iphone推送通知
我有一个iPhone应用程序是启用接收推送通知。 目前,我可以通过转到iphone设置/通知来禁用我的应用程序的推送通知。
但我想在我的应用程序中添加一个开关或button来启用或禁用推送通知。
这可以做到,因为我看到它在foursqure iPhone应用程序做到了。 他们在设置呼叫通知设置中获得了一个部分,用户可以为应用程序启用或禁用不同types的通知。
我看遍了networkingfind一个适当的解决scheme,但仍然没有find一个办法。 任何人都可以给任何想法如何做到这一点?
提前致谢 :)
首先,你can not enable and disable
在应用程序内can not enable and disable
push notification
。 如果您发现了一些应用程序,那么必须有解决方法。
就像你想要在应用程序内部做的那样,使用一个标识符并根据推送通知启用和禁用button将其发送到服务器。 所以,你的服务器端编码使用这个标识符,并根据这个工作。 像标识符是说,这是启用比你的服务器将发送通知否则不。
您可以使用以下代码检查用户设置是否enable
Push Notifications
。
启用或禁用Iphone推送通知
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (types == UIRemoteNotificationTypeNone) // Yes it is..
希望对你有帮助..
[ 仅供参考 – 很less有用户报告说它停止在iOS 10上工作 ]
您可以通过分别再次调用registerForRemoteNotificationTypes
和unregisterForRemoteNotificationTypes
来轻松启用和禁用应用程序中的推送通知。 我试过这个,它的工作原理。
斯威夫特4
启用推送通知(从应用程序安装):
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()