Мы пытались непрерывно устанавливать и удалять приложение ios и переключать уведомления, чтобы включить и установить для подписки значение true.Но делать это непрерывно, иногда отправляя подписку на один сигнал, не всегда удается.Ниже приведен код, который мы используем.
// Clicking button to enable push notification.
@IBAction func btnNotificationClicked(){
let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
let hasPrompted = status.permissionStatus.hasPrompted
if hasPrompted == false {
OneSignal.promptForPushNotifications(userResponse: { accepted in
if accepted == true {
DispatchQueue.main.asyncAfter(deadline: .now()+1.0, execute: {
OneSignal.consentGranted(true)
OneSignal.setSubscription(true)
})
}
})
} else {
if status.permissionStatus.status == OSNotificationPermission.authorized {
OneSignal.setSubscription(true)
} else if status.permissionStatus.status == OSNotificationPermission.denied {
displaySettingsNotification()
}
}
}
// Prompting user to enable to disable notification.
func displaySettingsNotification() {
let message = NSLocalizedString("Please turn on notifications by going to Settings > Notifications > Allow Notifications", comment: "Alert message when the user has denied access to the notifications")
let settingsAction = UIAlertAction(title: NSLocalizedString("Settings", comment: "Alert button to open Settings"), style: .`default`, handler: { action in
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
}
});
let okAction = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default) { (_) in
self.navigateToHealthController()
}
self.displayAlert(title: message, message: "", actions: [okAction, settingsAction]);
}
func displayAlert(title : String, message: String, actions: [UIAlertAction]) {
let controller = UIAlertController(title: title, message: message, preferredStyle: .alert);
actions.forEach { controller.addAction($0) };
self.present(controller, animated: true, completion: nil);
}
// One signal observer methods.
func onOSPermissionChanged(_ stateChanges: OSPermissionStateChanges!) {
if stateChanges.from.status == OSNotificationPermission.notDetermined {
if stateChanges.to.status == OSNotificationPermission.authorized {
navigateToHealthController()
removeIndicator()
OneSignal.setSubscription(true)
} else if stateChanges.to.status == OSNotificationPermission.denied {
removeIndicator()
}
}
}
func onOSSubscriptionChanged(_ stateChanges: OSSubscriptionStateChanges!) {
removeIndicator()
if stateChanges.from.subscribed && !stateChanges.to.subscribed { // NOT SUBSCRIBED != DENIED
Logger.print(items:"not subscribed")
} else if !stateChanges.from.subscribed && stateChanges.to.subscribed {
navigateToHealthController()
Logger.print(items:"subscribed successfully")
}
}