Push Plugin can't get registrationId value only

Hi!

I have the code from the docs, and I’m trying to access the registradionId property from the object registration.

But if I use the normal way to access the object, registration.registrationId, it says that I cant.

Is there any way to obtain only the registrationId, and not the object itself?? Actually thats what I did in ionic1 data.registrationId, but I can’t in Ionic2.

Thank you!!!

Hi, I use this code and works well in iOS and Android.

In my app.components.ts constructor I have this call this.initPushNotification();

And the code of this function is
> initPushNotification(){

    if (!this.platform.is('cordova')) {
        console.warn("Push notifications not initialized. Cordova is not available - Run in physical device");
        return;
    }
    let push = Push.init({
        android: {
            senderID: "XXXXXXXXX", //this.configApp.gcm_sender_id,  // Your sender id variable
            icon: 'icon',  // Small icon file name without extension
            iconColor: '#a4b1e2'  // Icon background color for Android 5 and newer
        },
        ios: {
            alert: "true",
            badge: false,
            sound: "true"
        },
        windows: {}
    });
    push.on('registration', (data) => {
        console.log("device token ->", data.registrationId);
        //TODO - send device token to server
        this.storage.set('pushRegisterDisabled', true);
        var oldRegId = localStorage.getItem('pushRegistrationId');
        if (oldRegId !== data.registrationId) {
            // Save new registration ID
            localStorage.setItem('pushRegistrationId', data.registrationId);
            // Post registrationId to your app server as the value has changed
        }
        // Sólo  si estamos autenticados, enviamos el PUSH TOKEN
         if (this.usuario) {
           let platformName = "Navegador";
           if ((<any>window).cordova) {
             if (this.platform.is('ios')) {
               platformName = "ios";
             } else if (this.platform.is('android')) {
               platformName = "android";
             }
           }
           // Se registra durante el login
           // We need to save registration id somewhere on server to be able to send push notifications to this device
           //this.storeDeviceToken(platformName.toLowerCase(), data.registrationId);
         }
    });
    push.on('notification', (data) => {
        console.log('message', data.message);
        //if user using app and push notification comes
        if (data.additionalData.foreground) {
            // if application open, show popup
            let confirmAlert = this.alertCtrl.create({
                title: 'New Notification',
                message: data.message,
                buttons: [{
                    text: 'Ignore',
                    role: 'cancel'
                }, {
                    text: 'View',
                    handler: () => {
                        //TODO: Your logic here
                        // self.nav.push(DetailsPage, {message: data.message});
                    }
                }]
            });
            confirmAlert.present();
        } else {
            //if user NOT using app and push notification comes
            //TODO: Your logic on click of push notification directly
            // self.nav.push(DetailsPage, {message: data.message});
            console.log("Push notification clicked");
        }
    });
    push.on('error', (e) => {
        console.log("PUSH error -> " + e.message);
    });
}

Best regards

Hi!

The thing is that I can’t access data.registrationId, it just says that parameter doesn’t exist, but in the object, is there.

But as you can see here, its there.

The only diference is the calling method

push.on(‘registration’, (data) => {

push.on(‘registration’).subscribe(data)

Ionic version?

I’m using ionic 2, I tried before your methods, but I cant use it.

I’m using the last release.

Cordova CLI: 6.5.0
Ionic Framework Version: 2.2.0
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 1.1.4
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Windows 10
Node Version: v6.10.0
Xcode version: Not installed

My config.xml has


Sorry I can`t see the problem

This is the error i get when I try to use push.on

I’m importing push

import {Push} from ‘ionic-native’;

And I don’t add to the constructor. I don’t use private push: Push. I use directly.

let push = Push.init({…

I’m using this version in config.xml

plugin name=“phonegap-plugin-push” spec="~1.10.0"

Hi!

Which version of ionic-native do you have? I can’t import Push the way you do it, because thats the old way, now should be imported with @ionic-native/push

Hi Mystearica,
I think, I have 3.4.4 version.

"@ionic-native/app-availability": "^3.4.4",
"@ionic-native/camera": "^3.4.4",
"@ionic-native/core": "^3.4.4",
"@ionic-native/file": "^3.4.4",
"@ionic-native/market": "^3.4.4",
"@ionic-native/media": "^3.4.4",
"@ionic-native/media-capture": "^3.4.4",

You should pass the type “any” to the data like:

pushy.on('registration').subscribe((data:any) => {
      //Your code
   })

Now it won’t error anymore :wink:

1 Like