AppDelegate.swift
func onOSSubscriptionChanged(_ stateChanges: OSSubscriptionStateChanges!) {
if !stateChanges.from.subscribed && stateChanges.to.subscribed {
print("Subscribed for OneSignal push notifications!")
}
if stateChanges.to.userId != nil {
AppData.sharedInstance.oneSignalToken = stateChanges.to.userId!
}
}
didFinishLaunchingWithOptions
OneSignal.initWithLaunchOptions(launchOptions, appId: ONE_SIGNAL_KEY)
OneSignal.inFocusDisplayType = .notification
handleOneSignal(launchOptions)
OneSignal.add(self as OSSubscriptionObserver)
Создать функцию
func handleOneSignal(_ launchOptions: [AnyHashable: Any]?) {
OneSignal.initWithLaunchOptions(launchOptions, appId:ONE_SIGNAL_KEY, handleNotificationReceived: { (notification) in
}, handleNotificationAction: { (result) in
let payload: OSNotificationPayload? = result?.notification.payload
var customKey: String? = payload?.body
if payload?.additionalData != nil {
var additionalData: [AnyHashable: Any]! = payload?.additionalData
if additionalData!["customeKey"] != nil {
customKey = customKey! + "\nPressed ButtonId:\(String(describing: additionalData!["customeKey"]))"
}
}
}, settings: [kOSSettingsKeyAutoPrompt : true])
}
Отправка push-уведомлений
let message="\(senderDisplayName!):\(text!)"
OneSignal.postNotification(["contents":["en":message], "include_player_ids": [self.userObj.userDeviceToken]])
Ссылка
https://documentation.onesignal.com/docs/ios-native-sdk#section--getpermissionsubscriptionstate-