【问题标题】:How to enable/disable push notification from the app?如何从应用程序启用/禁用推送通知?
【发布时间】:2012-11-28 16:12:08
【问题描述】:

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

【问题讨论】:

    标签: ios xcode push-notification unusernotificationcenter remote-notifications


    【解决方案1】:

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

    使用以下代码注册 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)];
    }
    

    并使用以下代码禁用它。

    [[UIApplication sharedApplication] unregisterForRemoteNotifications];
    

    【讨论】:

    【解决方案2】:

    斯威夫特 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-16
      • 2016-05-17
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 2022-12-10
      相关资源
      最近更新 更多