ios 如何启用/禁用来自应用程序的推送通知?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13814651/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:29:16  来源:igfitidea点击:

How to enable/disable push notification from the app?

iosxcodepush-notificationunusernotificationcenterremote-notifications

提问by Jeff

In my app I want to enable/disable push notification from the settings page of my app itself.Can any one suggest me a solution to turn on/off the status of app in notification center from the app ?

在我的应用程序中,我想从我的应用程序本身的设置页面启用/禁用推送通知。有人可以向我建议一种解决方案来打开/关闭应用程序通知中心中应用程序的状态吗?

回答by Paras Joshi

you can register and unregister the remote notification with bellow code.

您可以使用以下代码注册和取消注册远程通知。

Register RemoteNotification with bellow code..means Enable notification

使用下面的代码注册 RemoteNotification..意味着启用通知

//-- Set Notification
if ([[UIApplication sharedApplication]respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
    // For iOS 8 and above
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    // For iOS < 8
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

and Disable it with bellow code.

并使用波纹管代码禁用它。

[[UIApplication sharedApplication] unregisterForRemoteNotifications];

回答by Krunal

Swift 4

斯威夫特 4

Enable Push Notification (Setup from app):

启用推送通知(从应用程序设置):

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()
}

Delegate methods to handle push notifications

处理推送通知的委托方法

@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)
}

Disable Push Notifiacation:

禁用推送通知:

UIApplication.shared.unregisterForRemoteNotifications()