Save device token with FCM in ionic 2


#1

I am using FCM plugin to do push notification for ionic2.
reference : https://www.npmjs.com/package/cordova-plugin-fcm

I followed https://github.com/edismooth/ionic2-firebase/tree/master

It works fine and I can received the push from firebase console. Now I want to build my own server to let admin to send push notification with own backend.

I faced one problem is : I can get the device token, however, I have no idea how to save it to local storage. The code is as below. May I know which part I am wrong? Is that “this.local.set” can’t be used within the function of “FCMPlugin.getToken”. If yes, how can I save the token and use out of this function? Many thanks.

initializeApp() {
        this.platform.ready().then(() => {
            StatusBar.styleDefault();
            FCMPlugin.getToken(
                function (token) {
                    console.log(token); //I can get the token data
                    this.local.set('tokenvalue', token); // the token value can't be saved to local storage like this
                },
                function (err) {
                    console.log('error retrieving token: ' + err);
                }
            );
}

#2

To save to localstorage have a look here. It works great:

http://ionicframework.com/docs/v2/api/platform/storage/LocalStorage/


#3

I am able to use local storage in other places, just I don’t know why local storage can’t be used in my case. Thanks.


#4

Are you stringifying your data?


#5

@fishgrind
I tired, but not worked.
I have debug the APP on physical device with chrome inspect, it showed below error:
Error: Uncaught (in promise): TypeError: Cannot read property ‘local’ of null

Thanks.


#6

@eheading A bit late, but you can just change the code:

FCMPlugin.getToken(
	function (token) {
		console.log(token); //I can get the token data
		this.local.set('tokenvalue', token); // the token value can't be saved to local storage like this
	},
	function (err) {
		console.log('error retrieving token: ' + err);
	}
);

to:

FCMPlugin.getToken(
	(token) => {
		console.log(token); //I can get the token data
		this.local.set('tokenvalue', token); // the token value can't be saved to local storage like this
	},
	(err) => {
		console.log('error retrieving token: ' + err);
	}
);

so that this is correctly scoped.


#7

Am also having the same problem with the FCM plugin.

I want to build my own server to let admin to send push notification with own backend.

My problem is : I can get the device token, however, I have no idea how to save it to local storage. The code is as below. May I know which part I am wrong? Is that “this.local.set” can’t be used within the function of “FCMPlugin.getToken”. If yes, how can I save the token and use out of this function? Many thanks.

initializeApp() {
this.platform.ready().then(() => {
StatusBar.styleDefault();
FCMPlugin.getToken(
function (token) {
console.log(token); //I can get the token data
this.local.set(‘tokenvalue’, token); // the token value can’t be saved to local storage like this
},
function (err) {
console.log('error retrieving token: ’ + err);
}
);
}

If you solved this please advice…


#8

Never type the word “function” inside the body of one. Use arrow functions.


#9

thank you solved it.