Sometimes One Signal Returns Userid As Null
Solution 1:
When the user accepts push notification permissions, they will not instantly have a user ID. When the user accepts push notification permissions, the SDK sends out an HTTP request to register with OneSignal's server. Once that request is finished, the user subscription state is updated with a user ID.
I would suggest using the OSSubscriptionStateObserver protocol in order to get updated whenever the subscription state changes. You can access the user ID there.
publicclassMainActivityextendsActivityimplementsOSSubscriptionObserver {
protectedvoidonCreate(Bundle savedInstanceState) {
OneSignal.addSubscriptionObserver(this);
}
publicvoidonOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
if (!stateChanges.getFrom().isSubscribed() &&
stateChanges.getTo().isSubscribed()) {
newAlertDialog.Builder(this)
.setMessage("You've successfully subscribed to push notifications!")
.show();
// get player ID
stateChanges.getTo().getUserId();
}
Log.i("Debug", "onOSPermissionChanged: " + stateChanges);
}
}
From Onesignal library 4+ version there is a small tweak to this method.
public classMainActivityextendsActivityimplementsOSSubscriptionObserver {
protected void onCreate(Bundle savedInstanceState) {
OneSignal.addSubscriptionObserver(this);
}
public void onOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
if (!stateChanges.getFrom().getSubscribed() &&
stateChanges.getTo().getSubscribed()) {
new AlertDialog.Builder(this)
.setMessage("You've successfully subscribed to push notifications!")
.show();
// get player ID
stateChanges.getTo().getUserId();
}
Log.i("Debug", "onOSSubscriptionChanged: " + stateChanges);
}
}
/*
Example Logcat entry - User disabling notifications then returning to your app.
onOSSubscriptionChanged:
{"from":{"pushToken":"APA91bG9cmZ262s5gJhr8jvbg1q7aiviEC6lcOCgAQliEzHKO3eOdX5cm7IQqMSWfy8Od7Ol3jSjFfvCfeO2UYUpanJCURJ8RdhgEuV8grYxOCwPNJr5GoqcWTQOaL9u-qE2PQcFlv4K","userSubscriptionSetting":true,"subscribed":false},
"to": {"userId":"22712a53-9b5c-4eab-a828-f18f81167fef","pushToken":"APA91bG9cmZ262s5gJhr8jvbg1q7aiviEC6lcOCgAQliEzHKO3eOdX5cm7IQqMSWfy8Od7Ol3jSjFfvCfeO2UYUpanJCURJ8RdhgEuV8grYxOCwPNJr5GoqcWTQOaL9u-qE2PQcFlv4K","userSubscriptionSetting":true,"subscribed":true}}
Solution 2:
The method you are using is deprecated now.
Use use getPermissionSubscriptionState
, addPermissionObserver
, or add SubscriptionObserver
instead
Here is a sample -
OSPermissionSubscriptionStatestatus= OneSignal.getPermissionSubscriptionState();
booleanisEnabled= status.getPermissionStatus().getEnabled();
booleanisSubscribed= status.getSubscriptionStatus().getSubscribed();
booleansubscriptionSetting= status.getSubscriptionStatus().getUserSubscriptionSetting();
StringuserID= status.getSubscriptionStatus().getUserId();
For more reference, check this
Here is the issue on Github
Solution 3:
The problem with onOSSubscriptionChanged is that it ONLY gets called when the status of the OneSignal subscription gets changed(Enabled/Disabled) - as the name says -.
So, the better way to get the userId/playerId would to get the it from the getSubscriptionStatus' JSONObject.
privateStringgetOneSignalUserID() {
JSONObject subscriptionStatusJsonObject = OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().toJSONObject();
String userId = "";
try {
userId = subscriptionStatusJsonObject.getString("userId");
} catch (JSONException e) {
e.printStackTrace();
}
return userId;
}
Post a Comment for "Sometimes One Signal Returns Userid As Null"