Retrieve device token from device using ionic framework

17,851

Solution 1

I've used phonegap-plugin-push plugin and its pretty easy and simple. For regID in the code on deviceReady event I've used.

var push = PushNotification.init({
        "android": {
            "senderID": "SENDER-ID"
        },
        "ios": {"alert": "true", "badge": "true", "sound": "true"}, 
        "windows": {} 
    });

    push.on('registration', function(data) {
        console.log("registration event");
        //here is your registration id
        console.log(data.registrationId);
    });

This is the link of the tutorial too

Hope it helps.

Solution 2

Here is the ng-Cordova document which is for getting current device token id.

copy and past it in your project controller.

In the given below line you can find notification parameter

$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) 

The notification is an object and it will have an regid field which provides you the current mobile device id you can make use of that like (posting to server, view in console.log(notification);)

To send the device token to you server you need just do

var loginPost = {
                    "UserName":"Mike",
                    "PassWord":"xxxxxx",
                    "DeviceID":notification.regid
                  };

Using this loginPost variable object post to the server.

Share:
17,851
Manoj Rejinthala
Author by

Manoj Rejinthala

Updated on June 14, 2022

Comments

  • Manoj Rejinthala
    Manoj Rejinthala almost 2 years

    I am using ionic to develop an application and I need to get the device token for push notifications, but having trouble. The application isn't receiving notifications, but I still need to send device token when user login into the application (e.g. I need to get device token without receiving notifications). I tried using the Cordova push notification plugin also.

  • Manoj Rejinthala
    Manoj Rejinthala about 8 years
    what is "senderID": "replace_with_sender_id"
  • Mohan Gopi
    Mohan Gopi about 8 years
    @monoj rejinthala that is your project number which you have created in google cloud should be your senderId
  • Manoj Rejinthala
    Manoj Rejinthala about 8 years
    if i want just device token should i need to clone the google cloud and follow the remaining steps or just register the app
  • Mohan Gopi
    Mohan Gopi about 8 years
    to send a push notification ofcourse you need device token but it is not manditory to clone with GCM but you should clone project number and your server key to your current project.
  • M. Junaid Salaat
    M. Junaid Salaat about 8 years
    Its nothing.. Just replace it with your sender id.
  • Manoj Rejinthala
    Manoj Rejinthala about 8 years
    i don't have any notification yet just i need to send device token when user login into the application that sit.i need to get device token with out any notification.
  • Manoj Rejinthala
    Manoj Rejinthala about 8 years
    every time it returns different value i think device token is unique for device am i right
  • pbaranski
    pbaranski over 7 years
  • Sayed Mohd Ali
    Sayed Mohd Ali over 4 years
    not working on with ionic v3 error: Property 'on' does not exist on type 'Push'.ts(2339
  • M. Junaid Salaat
    M. Junaid Salaat over 4 years
    @SayedMohdAli This is for Ionic v1 only. as it is ngcordova.
  • Sayed Mohd Ali
    Sayed Mohd Ali over 4 years
    @junaid ok thanks, i think I have to do with FCM in ionic v3