【问题标题】:Detect click when the allow Button in the notification alert permission is tapped点击通知警报权限中的允许按钮时检测点击
【发布时间】:2021-09-13 09:31:59
【问题描述】:

我正在使用 SwiftUI 开发这个 iOS 应用。

如何检测用户何时单击通知权限警报的允许按钮?它应该只发生一次,我需要在用户第一次点击按钮时将其带到特定的选项卡。这可能吗?

【问题讨论】:

    标签: swift push-notification notifications popup alert


    【解决方案1】:

    您可以添加属性onclick="location.href='https://google.com';"。

    但是你能发布 coden-ps 吗?你使用javascript和php吗?

    【讨论】:

    • 我正在使用 Swift
    【解决方案2】:

    我找到了解决方案。为了创建这种行为,我只是在 UserDefault 中保存了一个 Bool 值。

     func registerForPushNotifications() {
        UNUserNotificationCenter.current()
            .requestAuthorization(options: [.alert, .sound, .badge]) { [weak self] granted, _ in
                print("Permission granted: \(granted)")
                
                self?.userDefault.set(granted, forKey: "notificationGranted")
    
                guard granted else {
                    return
                }
    
                self?.getNotificationSettings()
            }
    }
    
          func getNotificationSettings() {
        UNUserNotificationCenter.current().getNotificationSettings { settings in
            print("Notification settings: \(settings)")
            
            guard settings.authorizationStatus == .authorized else {
                return
            }
            // Saving the value to permit to take the user to the Notification screen only the first time
            if (self.userDefault.value(forKey: "permissionPopUpIsShowed") == nil) {
                self.userDefault.set(true, forKey: "permissionPopUpIsShowed")
            }
            
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    }
    

    在 onAppear 方法内的 Content View 中,您可以这样做:

                            if appDelegate.userDefault.bool(forKey: "permissionPopUpIsShowed") == true && appDelegate.userDefault.bool(forKey: "notificationGranted") == true {
                            tabSelection = .notifications
                            appDelegate.userDefault.set(false, forKey: "permissionPopUpIsShowed")
                        }
    

    通过这种方式,您将能够选择一个新视图(通过 tabSelection)并仅获得一次此行为(当显示通知权限弹出窗口时)。

    【讨论】:

      猜你喜欢
      • 2014-11-24
      • 2018-05-06
      • 2019-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      相关资源
      最近更新 更多