El Dude
El Dude

Reputation: 5618

ionic2 push get token

Is there a way to get the token outside the register method later in the code? I mean, is the token assigned to an attribute of the Push object somewhere?

  push.on('registration', (data) => {
    // console.log("device token ->", data.registrationId);
    //TODO - send device token to server
  });

The above code works to see the token, yet it is outside of my controllers or providers. I have to access it later in my code. I want to send the token to server, but for this I first have to get the user name. Since push registration happens somewhen when device ready, I yet do not have access to the user name. Another problem is for new users where token can not be assigned to a specific user yet and send device token to server can not be executed.

Upvotes: 1

Views: 419

Answers (1)

Tomislav Stankovic
Tomislav Stankovic

Reputation: 3128

You can use localStorage.

setItem

localStorage.setItem('device_token', data.registrationId);

and use it like this

getItem

 this.device_token = localStorage.getItem('device_token');

Upvotes: 1

Related Questions