ios 从 iPhone 应用程序启用/禁用 Apple 推送通知?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10493949/
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/Disable Apple Push Notification from iPhone app?
提问by Gopinath
I have one more doubt on APNS
. That is when the app first launch the app asks for Apple Push Notification Permission if the user accepted the they can receive the notifications. If the user cancelled they can't receive any notifications. Am I clear??
我还有一个疑问APNS
。那是当应用程序首次启动时,如果用户接受他们可以接收通知,则应用程序会要求 Apple 推送通知权限。如果用户取消,他们将无法收到任何通知。我清楚吗??
Now my doubt is,
现在我的疑问是,
At first time if the user cancelled the push notification service from the app (Clicked
Cancel
button) again after some days if they want receive Apple Push Notification it is possible to enable the Apple Push Notification again for the particular user from the App.And if the user accept the apple push notification service first and after some days if they don't want to receive the notifications it is possible to disable the
APNS
in our app? I hope you understand my doubt. Can any one please clarify this doubt?It is possible to do these above scenarios in our iPhone app?
第一次,如果用户
Cancel
在几天后再次从应用程序取消推送通知服务(单击按钮),如果他们想收到 Apple 推送通知,则可以从应用程序为特定用户再次启用 Apple 推送通知。如果用户先接受苹果推送通知服务,几天后如果他们不想收到通知,是否可以
APNS
在我们的应用程序中禁用?我希望你能理解我的疑惑。任何人都可以澄清这一疑问吗?可以在我们的 iPhone 应用程序中执行上述这些场景吗?
Please help me. Thanks in advance.
请帮我。提前致谢。
采纳答案by adig
Unfortunately you can't enable or disable the Push Notifications for your app from the app code. The dialog asking for permission is displayed only once. Usually, other apps display instructions to the user to enable / disable push notifications by going into Settings-> Notification-> AppName.
不幸的是,您无法通过应用代码为您的应用启用或禁用推送通知。请求许可的对话框只显示一次。通常,其他应用程序通过进入设置-> 通知-> AppName 向用户显示启用/禁用推送通知的说明。
回答by jrtc27
You can read your app's permissions using UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
and then performing a bitwise and operation with the different types to see which are enabled. You can also call unregisterForRemoteNotifications
to disable notifications. The one thing you can't do is turn on notifications, although you can direct the user.
您可以使用读取应用程序的权限UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
,然后使用不同类型执行按位和操作以查看哪些已启用。您也可以调用unregisterForRemoteNotifications
以禁用通知。您不能做的一件事是打开通知,尽管您可以引导用户。
回答by arshad
My requirement was to enable and disable pushnotificaton with a UISwitch
.Inorder to enable push notification from the codeuse this inside the button action.
我的要求是使用UISwitch
.Inorder启用和禁用 pushnotificaton 。为了启用来自代码的推送通知,请在按钮操作中使用它。
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
Inorder to disable
为了禁用
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
NSLog(@"UnRegistered for pushnotification");
回答by Malek_Jundi
1.From your app No its just appear the first time the user open your app after install it .. if then he decide to allow it he can activated from device settings.
1.从你的应用程序 不,它只是在用户安装后第一次打开你的应用程序时出现..如果他决定允许它,他可以从设备设置中激活。
2.it can be done from the app and settings .. if you want to disable it from your app you could send the device token (who decide to disable the push notification) to your server and store it for ex. as "no notification list" and when send the payload you ignore these tokens so they will not receive the notification.
2.它可以从应用程序和设置中完成..如果您想从您的应用程序中禁用它,您可以将设备令牌(决定禁用推送通知的人)发送到您的服务器并将其存储以备不时之需。作为“无通知列表”,当发送有效负载时,您会忽略这些令牌,因此它们将不会收到通知。
3.I already answer it.
3.我已经回答了。
Good Luck.
祝你好运。
回答by wasim
When you give permission for the first time the device token of your iPhone is registered with the APN server and then you can receive the push notification. Later you can enable/disable from your device settings → notification → your app.
当您第一次授予权限时,您的 iPhone 的设备令牌已在 APN 服务器上注册,然后您就可以收到推送通知。稍后您可以从您的设备设置 → 通知 → 您的应用程序启用/禁用。
回答by debiasej
You can use this code to give support in iOS 9
您可以使用此代码在 iOS 9 中提供支持
if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {
UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
if (types == UIUserNotificationTypeNone) {
// Do something
NSLog(@"");
}
} else {
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) {
// Do something
NSLog(@"");
}
}
see How to update code using enabledRemoteNotificationTypes because it is "not supported in iOS 8"?
请参阅如何使用 enabledRemoteNotificationTypes 更新代码,因为它“在 iOS 8 中不受支持”?
回答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()