2

I'm trying to add push notification to my mobile native chat app. I'm trying to use OneSignal.

I can send manual push notification, so I think gradle part is okay

idsAvaiable method is deprecated, I started to looking for how can I get userId.

OSPermissionSubscriptionState status = OneSignal.getPermissionSubscriptionState();
    String userId = status.getSubscriptionStatus().getUserId();

In here, I'm trying to get userId with status, but it's saying:

Cannot resolve symbol 'OSPermissionSubscriptionState'

How can I get userId?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

1

Root cause

From OneSignal API 4.0.0, there are many APIs that have been removed including OSPermissionSubscriptionState.

Solution 1

Use OneSignal.getDeviceState()

OSDeviceState device = OneSignal.getDeviceState();
String userId = device.getUserId();

Solution 2

Use OneSignal.addSubscriptionObserver()

OneSignal.addSubscriptionObserver(new OSSubscriptionObserver() {

    @Override
    public void onOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
        if (!stateChanges.getFrom().isSubscribed() && stateChanges.getTo().isSubscribed()) {
            // Get user id
            String userId = stateChanges.getTo().getUserId();
        }
    }
});

For more information, see the change log here.

Son Truong
  • 13,661
  • 5
  • 32
  • 58