简体   繁体   English

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

[英]How to enable/disable push notification from the app?

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

you can register and unregister the remote notification with bellow code. 您可以使用以下代码注册和取消注册远程通知。

Register RemoteNotification with bellow code..means Enable notification 使用以下代码注册RemoteNotification.means启用通知

//-- 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];

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从我的应用启用/禁用推送通知 - How to enable/disable push notification from my app 从iPhone应用程序启用/禁用Apple推送通知? - Enable/Disable Apple Push Notification from iPhone app? 如何仅为该应用禁用来自该应用的推送通知? - How to disable push notification from app for that app only? 如何从 iOS Swift 5 中的应用设置启用推送通知? - How to enable push notification from app settings in iOS Swift 5? 是否可以从iOS 7上的应用程序中禁用推送通知声音? - Is it possible to disable push notification sounds from within my app on iOS 7? 完成启动后或在我们的应用程序内部启用/禁用推送通知 - Enable/Disable push notification after did finish launch or inside our app 如何在用户点按推送通知时禁用打开应用 - How to disable opening app when user tap push notification 如何在收到推送通知并打开应用程序时禁用警报? - How to disable alert when push notification is received and app is opened? 在应用程序内启用或禁用 iPhone 推送通知 - Enable or Disable Iphone Push Notifications inside the app 如何将用户从推送通知警报重定向到App Store上的应用程序? - How to redirect users to app on the App Store from push notification alert?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM