CottaLotties
CottaLotties

Reputation: 106

Is that possible to get device token using OneSignal

Maybe someone knows how to get device token when using OneSignal to receive notifications?

Upvotes: 2

Views: 6988

Answers (4)

Smiter
Smiter

Reputation: 1197

For React Native getDeviceState is no longer a function on the latest OneSignal v5.0.3. Here is how you get the subscription ID and subscription token for the user after they have accepted permission:

const subId = OneSignal.User.pushSubscription.getPushSubscriptionId();
const subToken = OneSignal.User.pushSubscription.getPushSubscriptionToken();

Upvotes: 1

Remoo
Remoo

Reputation: 747

For Flutter use this code:

OneSignal.shared.getDeviceState().then((deviceState) {
     final pushToken = deviceState.pushToken;
}

Upvotes: 1

Nicolai Lissau
Nicolai Lissau

Reputation: 8332

If you are using the react native SDK you can retrieve it from the device state:

const pushToken = (await OneSignal.getDeviceState()).pushToken

Upvotes: 3

CottaLotties
CottaLotties

Reputation: 106

This is it:

OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
            @Override
            public void idsAvailable(String userId, String registrationId) {
                Log.d("debug", "User:" + userId);
                if (registrationId != null)
                    Log.d("debug", "registrationId:" + registrationId);

            }
        });

It helps to get user id and that is actually a device token.

Upvotes: 5

Related Questions