ios 在应用程序内启用或禁用 iPhone 推送通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10510578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable or Disable Iphone Push Notifications inside the app
提问by Sameera Chathuranga
I have a iphone app which is enable to receive push notifications. Currently i can disable push notifications for my app by going to the iphone settings/Notifications.
我有一个可以接收推送通知的 iphone 应用程序。目前,我可以通过进入iPhone设置/通知禁用推送通知我的应用程序。
But i want to add a switch or button inside my app to enable or disable push notifications.
但我想在我的应用程序中添加一个开关或按钮来启用或禁用推送通知。
It can be done because i saw it in foursqure iphone app did it. They got a section in settings call notification settings and user can enable or disable different kind of notification for the app.
它可以完成,因为我在foursqure iphone应用程序中看到它做到了。他们在设置呼叫通知设置中有一个部分,用户可以启用或禁用应用程序的不同类型的通知。
I look all over the net to find a proper solution for this but still not found a way. Can any one please give any idea how to do that ?
我在网上寻找合适的解决方案,但仍然没有找到方法。任何人都可以提供任何想法如何做到这一点?
Thanks in advance :)
提前致谢 :)
采纳答案by Nitin
First thing is that you can not enable and disable
push notification
in inside the app. If you have found some apps which did it than there must be workaround solution.
首先是你can not enable and disable
push notification
在应用程序里面。如果您找到了一些应用程序,那么必须有解决方法。
Like if you want to do Inside the app then use one identifier and send it to server according push notification enable and disable button. So, your server side coding use this identifier and work according to that. Like identifier is say it's enable than your server will send notification otherwise not.
就像如果你想在应用程序内部做,然后使用一个标识符并根据推送通知启用和禁用按钮将其发送到服务器。因此,您的服务器端编码使用此标识符并据此工作。就像标识符一样,它已启用,而不是您的服务器将发送通知,否则不会。
You can check that user set enable
or disable
Push Notifications
using following code.
您可以检查该用户集enable
或disable
Push Notifications
使用以下代码。
Enable or Disable Iphone Push Notifications
启用或禁用 iPhone 推送通知
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone)
// Yes it is..
Hope, this will help you..
希望能帮到你..
回答by Varun Bhatia
[FYI - Few users have reported that it stopped working on iOS 10]
[仅供参考 - 很少有用户报告它停止在 iOS 10 上运行]
You can easily enable and disable push notifications in your application by calling registerForRemoteNotificationTypes
and unregisterForRemoteNotificationTypes
respectively again. I have tried this and it works.
您可以通过分别再次调用registerForRemoteNotificationTypes
和来轻松启用和禁用应用程序中的推送通知unregisterForRemoteNotificationTypes
。我试过这个,它的工作原理。
回答by Krunal
Pragmatically, it is possible to enable & disable push notification by registering and unregistering push notification.
实际上,可以通过注册和取消注册推送通知来启用和禁用推送通知。
Enable Push Notification:
启用推送通知:
if #available(iOS 10.0, *) {
// 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{
// Below iOS 10.0
let settings = UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
//or
//UIApplication.shared.registerForRemoteNotifications()
}
Delegate methods
委托方法
@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) {
// .. Receipt of device token
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
// handle error
}
Disable Push Notification:
禁用推送通知:
UIApplication.shared.unregisterForRemoteNotifications()
回答by Dyary
if you are using FireBase
to send push notifications to your devices , you can use topic subscription to enable push notification in subscribed devices and unsubscribe users from the topic when you don't want the user to receive push notification in those devices that have been unsubscribed.
如果您使用的是FireBase
发送推送通知到您的设备,您可以使用主题订阅启用订阅设备,并从主题的订阅用户推送通知,当你不希望用户接收那些已经退订设备推送通知。
to subscribe a user to a topic simply import Firebase, then use this method:
要将用户订阅到主题,只需导入 Firebase,然后使用此方法:
Messaging.messaging().subscribe(toTopic: "topicName")
and to unsubscribe a user use:
并取消订阅用户使用:
Messaging.messaging().unsubscribe(fromTopic: "topicName")