首页 文章

从iPhone应用程序启用/禁用Apple推送通知?

提问于
浏览
20

我对 APNS 还有一个疑问 . 这是当应用程序首次启动应用程序请求Apple推送通知权限时,如果用户接受了他们可以接收通知 . 如果用户取消他们无法收到任何通知 . 我清楚吗?

现在我的怀疑是,

  • 如果用户第一次取消推送通知服务(几次点击 Cancel 按钮),如果他们想要接收Apple推送通知,则可以再次从应用程序为特定用户启用Apple推送通知 .

  • 如果用户首先接受苹果推送通知服务,如果他们不想接收通知,可以在我们的应用程序中禁用 APNS ?我希望你理解我的怀疑 . 任何人都可以澄清这个疑问吗?

  • 可以在我们的iPhone应用程序中执行以上方案吗?

请帮我 . 提前致谢 .

7 回答

  • 0

    很遗憾,您无法从应用代码启用或禁用应用的推送通知 . 请求权限的对话框仅显示一次 . 通常,其他应用程序通过进入Settings-> Notification-> AppName向用户显示启用/禁用推送通知的指令 .

  • 16

    您可以使用 UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; 读取应用程序的权限,然后使用不同类型执行按位和操作以查看哪些已启用 . 您也可以调用 unregisterForRemoteNotifications 来禁用通知 . 您不能做的一件事是打开通知,尽管您可以指导用户 .

  • 15

    我的要求是使用 UISwitch 启用和禁用pushnotificat . . 按顺序 enable push notification from the code 在按钮操作中使用它 .

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
       (UIRemoteNotificationTypeBadge |  UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    

    Inorder to disable

    [[UIApplication sharedApplication] unregisterForRemoteNotifications];
    NSLog(@"UnRegistered for pushnotification");
    
  • 4

    1.从你的应用程序中取出它只是在用户首次安装后打开你的应用程序时出现..如果他决定允许它,他可以从设备设置激活 .

    2.可以从应用程序和设置中完成..如果要从应用程序中禁用它,您可以将设备令牌(决定禁用推送通知)发送到您的服务器并将其存储为ex . 作为“无通知列表”,当发送有效负载时,您忽略这些令牌,以便他们不会收到通知 .

    我已经回答了 .

    祝好运 .

  • 0

    当您第一次给予权限时,iPhone的设备令牌已在APN服务器上注册,然后您就可以收到推送通知 . 稍后您可以通过设备设置→通知→您的应用启用/禁用 .

  • 1

    您可以使用此代码在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(@"");
        }
    }
    

    How to update code using enabledRemoteNotificationTypes because it is "not supported in iOS 8"?

  • 1

    实际上,可以通过注册和取消注册推送通知来启用和禁用推送通知 .

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

相关问题