我正在尝试为我的应用使用OneSignal IOS Native SDK
。我在课程中加入了OSPermissionObserver and OSSubscriptionObserver
。它需要添加协议存根,并且在协议内部,我需要在代码中调用allowNotificationSwitch
。但是我不知道该怎么办。
这就是我的TableViewCell
的样子。 中的
indexPath.row = 0
有Switch
个对象。由于indexPath.row 0
仅具有switch对象,因此我以编程方式完成了此操作。我使用下面的代码在cellRowAt
内创建开关并创建了switchChanged
函数。问题是如何在OSPermissionObserver和OSSubscriptionObserver协议内调用allowNotificationSwitch
?我注释掉了我将称为allowNotificationSwitch
的部分。希望我能以您能理解的方式进行解释。希望能为您提供帮助。非常感谢。
AllowNotificationSwitch
if indexPath.row == 0 {
let allowNotificationSwitch = UISwitch(frame: .zero)
allowNotificationSwitch.setOn(false, animated: true)
allowNotificationSwitch.tag = indexPath.row
allowNotificationSwitch.addTarget(self, action: #selector(self.switchChanged(_:)), for: .valueChanged)
cell.accessoryView = allowNotificationSwitch
}
@objc func switchChanged(_ sender : UISwitch!) {
let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
let isSubscribed = status.subscriptionStatus.subscribed
if isSubscribed == true {
sender.isOn = true
sender.isUserInteractionEnabled = true
}
OneSignal.add(self as OSPermissionObserver)
OneSignal.add(self as OSSubscriptionObserver)
if !sender.isOn {
OneSignal.setSubscription(false)
} else {
OneSignal.setSubscription(true)
}
print("table row switch change \(sender.tag)")
print("the switch is \(sender.isOn ? "ON" : "OFF")")
}
OSPermissionObserver和OSSubscriptionObserver协议(基于OneSignal文档)
func onOSPermissionChanged(_ stateChanges: OSPermissionStateChanges!) {
if stateChanges.from.status == OSNotificationPermission.notDetermined {
if stateChanges.to.status == OSNotificationPermission.authorized {
// allowNotificationsSwitch.isUserInteractionEnabled = true
} else if stateChanges.to.status == OSNotificationPermission.denied {
displaySettingsNotification()
}
} else if stateChanges.to.status == OSNotificationPermission.denied { // DENIED = NOT SUBSCRIBED
// allowNotificationsSwitch.isUserInteractionEnabled = false
}
}
func onOSSubscriptionChanged(_ stateChanges: OSSubscriptionStateChanges!) {
if stateChanges.from.subscribed && !stateChanges.to.subscribed { // NOT SUBSCRIBED != DENIED
// allowNotificationsSwitch.isOn = false
} else if !stateChanges.from.subscribed && stateChanges.to.subscribed {
// allowNotificationsSwitch.isOn = true
// allowNotificationsSwitch.isUserInteractionEnabled = true
}
}