IOSでOneSignalユーザーの一意のプレーヤーIDを取得する方法OneSignalの公式ドキュメントにiOS SDKセットアップしかありません。
何か提案があったらありがとう。
OSSubscriptionObserverなどのOneSignalのオブザーバーを使用する必要があります。
// Add OSSubscriptionObserver after UIApplicationDelegate
class AppDelegate: UIResponder, UIApplicationDelegate, OSSubscriptionObserver {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Add your AppDelegate as an subscription observer
OneSignal.add(self as OSSubscriptionObserver)
}
// After you add the observer on didFinishLaunching, this method will be called when the notification subscription property changes.
func onOSSubscriptionChanged(_ stateChanges: OSSubscriptionStateChanges!) {
if !stateChanges.from.subscribed && stateChanges.to.subscribed {
print("Subscribed for OneSignal Push notifications!")
}
print("SubscriptionStateChange: \n\(stateChanges)")
//The player id is inside stateChanges. But be careful, this value can be nil if the user has not granted you permission to send notifications.
if let playerId = stateChanges.to.userId {
print("Current playerId \(playerId)")
}
}
}
より良い説明のために、ここに addSubscriptionObserver のドキュメントがあります
プレイヤーID(またはユーザーID)をコード内のどこかに取得する必要があるので、どこにも保存したくありません。
私はこのコードを使用してしまいました:
let userId = OneSignal.getPermissionSubscriptionState().subscriptionStatus.userId
let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
let hasPrompted = status.permissionStatus.hasPrompted
print("hasPrompted = \(hasPrompted)")
let userStatus = status.permissionStatus.status
print("userStatus = \(userStatus)")
let isSubscribed = status.subscriptionStatus.subscribed
print("isSubscribed = \(isSubscribed)")
let userSubscriptionSetting = status.subscriptionStatus.userSubscriptionSetting
print("userSubscriptionSetting = \(userSubscriptionSetting)")
let userID = status.subscriptionStatus.userId // This one
print("userID = \(userID)")
let pushToken = status.subscriptionStatus.pushToken
print("pushToken = \(pushToken)")