问题
In my application i am sending notifications to the user through one signal.but sometimes one Signal returns null User Id...that time i am facing problem of sending notification to user.how can i resolve it. if anyone have solution, please share with me
here is my code for generating user Id
OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
@Override
public void idsAvailable(String userId, String registrationId) {
Log.d("test","key is"+notification_key);
notification_key=userId;
}
});
and i am intialising onesignal like this in MyApplication.class
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
回答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.
public class MainActivity extends Activity implements OSSubscriptionObserver {
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", "onOSPermissionChanged: " + stateChanges);
}
}
回答2:
The method you are using is deprecated now.
Use use getPermissionSubscriptionState
, addPermissionObserver
, or add SubscriptionObserver
instead
Here is a sample -
OSPermissionSubscriptionState status = OneSignal.getPermissionSubscriptionState();
boolean isEnabled = status.getPermissionStatus().getEnabled();
boolean isSubscribed = status.getSubscriptionStatus().getSubscribed();
boolean subscriptionSetting = status.getSubscriptionStatus().getUserSubscriptionSetting();
String userID = status.getSubscriptionStatus().getUserId();
For more reference, check this
Here is the issue on Github
回答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.
private String getOneSignalUserID() {
JSONObject subscriptionStatusJsonObject = OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().toJSONObject();
String userId = "";
try {
userId = subscriptionStatusJsonObject.getString("userId");
} catch (JSONException e) {
e.printStackTrace();
}
return userId;
}
来源:https://stackoverflow.com/questions/54475499/sometimes-one-signal-returns-userid-as-null