4

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();

3 Answers3

7

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().isSubscribed() &&
        stateChanges.getTo().isSubscribed()) {
         new AlertDialog.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 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", "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}}
Tony Lip
  • 554
  • 6
  • 14
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

Kartik Shandilya
  • 3,796
  • 5
  • 24
  • 42
1

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;
}
Syed Arsalan Kazmi
  • 949
  • 1
  • 11
  • 17