fechanique / cordova-plugin-fcm

Google FCM Push Notifications Cordova Plugin
624 stars 990 forks source link

How to use the getToken() function ? #379

Closed ghost closed 6 years ago

ghost commented 6 years ago

Hello everyone,

I want to send notification to a specific user. I've seen that it can be possible by using the getToken() function. But i'm newer to cordova & Js stuff, and the readme section doesn't help me much.

I know how tokens (in general) works but i don't know how to implement it in my code.

So, is someone can help me to understand how the getToken() function works ?

Thanks a lot for your help ! have a good day ;)

Baadier-Sydow commented 6 years ago

This isnt really the right place to ask this kind of question as its meant for reporting issues as opposed to asking for general help.

You use getToken() to register the device with FCM. It returns a token that you can use to target the device.

However, before you can start sending messages to the device you will need to do the FCM specific setup ie setting up a project, including the FCM config files in your project and then rebuilding.

Once that's done you should be able to test sending notifications via https://cordova-plugin-fcm.appspot.com/.

ghost commented 6 years ago

Hello and thank you for your post.

I have already set up a project, downloaded the config file that i've put in my project, and able to receive notifications on my phone with my app (thanks to the firebase console). I've done that just for android.

By the way, I visited your link, but it seems that i can't receive the notification sended by the cordova tester. I've put the right key and the right application, the pop-up shows up to say "Push notification sent successfully! ", but nothing on my phone...

How is that possible ?

Baadier-Sydow commented 6 years ago

So to clarify, when you send via the Firebase Console it goes through?

Are you using the recipient as a topic or not on the page I linked

ghost commented 6 years ago

Yes, via Firebase Console, it goes through.

Then, after unistall/reinstall of the plugin, I can now receive notifications on my phone by the cordova tester (your link) only if recipient is a topic !

So, when i read the readme section, "All devices are subscribed automatically to 'all' and 'ios' or 'android' topic respectively. Must match the following regular expression: "[a-zA-Z0-9-_.~%]{1,900}"", does that mean that i can create a topic with the name or/and the phone number of my contact and send him a specific notification ?

Maybe i'm too quick, but i want to understand to create a good app, you know ?

Thanks :)

Baadier-Sydow commented 6 years ago

You would register your device and then just attach the token you receive to the user you're working with so you can target that specific user. I see topics as something you send to a group vs an individual recipient.

Their used to be a limit on how many topics you could have but that is no longer the case.

What you've said thus far seems to indicate that your device is correctly registered on FCM, as the message goes through on the topics, but you're not targeting the correct recipient when you test.

Are you getting the token that is sent back in the getToken method?

ghost commented 6 years ago

Ok, all i can tell you is that when i put FCMPlugin.getToken(function(token){alert(token);}); in my deviceready event listener (for example), i get on my phone, an alert with the long ID of the token.

Is that the information you want ?

And, what you've said about "not targeting the correct recipient", will it be the place to put the token to target the good contact ?

Baadier-Sydow commented 6 years ago

That is correct.

FCMPlugin.getToken(function(token){
    alert(token);
});

getToken resolves with the token that you need to target the specific device. That is the unique device registration on FCM.

You use that to target that specific device.

So using the token you receive you can add it as the recipient on https://cordova-plugin-fcm.appspot.com/ and deselect Recipient is a topic and you should be good to go.

It sounds like you're almost there.

ghost commented 6 years ago

Ok, i've tried to do that, but the sent notification never came to my phone.

Maybe i've done some mistakes in the copy of the token ID, but lower "L" and upper "i" are indistinguishable...

Is the notification sent by the fcm tester is suppose to come to my phone ?

Baadier-Sydow commented 6 years ago

ehm how are you copying the token code? Manually typing it out?

Android push notifications will work in the simulator. Why not console.log() the token then copy it from the Console output to the FCM tester. That prevents any copy errors.

You wont see anything coming through unless you either minimize the application of have something explicitly handle the receipt of a new token. I would suggest just minimizing the app and seeing if it comes through.

ghost commented 6 years ago

I've tried to run the app in the ripple emulator. it comes with an error.

Then, i tried on the android emulator, and it comes that the value of the token is "null".

And no error in my output console...

Baadier-Sydow commented 6 years ago

Use Genymotion as a emulator. Do you have a console.log(token); in the function you pass in as a callback to the getToken method?

Are using Ionic or some other framework?

ghost commented 6 years ago

Yep i have a console.log(token); or a alert(token); when i'm on my phone. And i use the jQuery mobile framework.

I'll try with genymotion :)

Baadier-Sydow commented 6 years ago

Does your app get built using Cordova?

ghost commented 6 years ago

Do you mean that the app can built on my phone ? yes, if this is what you ask

And, how can i run my project in genymotion? i've installed it, and add an emulator. I'm in the Genymotion shell with the right emulator, but i can't do Cordova run android and other stuff like that...

By the way, my project is a visual studio solution

Baadier-Sydow commented 6 years ago

I'm a bit confused. Do you end up with a APK file when you build your project?

You should be able to just drag this file onto an active emulator window and it will be installed.

Then run the app and open Chrome and navigate to Chrome Inspect to debug the application.

You'll be able to see all the console logs doing this.

Which framework are you using? Why are you not using something like Ionic that will make this kind of set up easier?

ghost commented 6 years ago

no, i'haven't any APK created. Visual studio doesn't make an APK.

I'll try to create an APK.

I use the jQuery mobile framework because this is not a very big project.

Baadier-Sydow commented 6 years ago

I'm not sure at this point if you're using Cordova as this plugin requires Cordova?

I'm under the impression jQuery Mobile is for web based projects that run in mobile browsers as opposed to being deployed to the Play or App Store.

ghost commented 6 years ago

ok, i have managed to make an APK out of my project. but the problem is that the value of the token is still null.

And JQuery mobile is fine with cordova. You can make an app with jquery & cordova that work fine !

I'll try to copy the token with an other maner. If my phone can't receive the notification send by the fcm tester, and with the right token, what kind of problem is it ? have you an idea ?

ghost commented 6 years ago

Yop ! i've managed to have my token and paste it in the fcm tester !!!

Then, i received the right notification on my phone via the fcm tester.

Everything seems to work !

So, can i send a notification now, or there is more stuff to do before ?

Baadier-Sydow commented 6 years ago

Congrats :)

Sounds like you're all good now!

Just put something in to handle the token refresh event and you're all set.

ghost commented 6 years ago

Ok, thanks a lot for your help ! :)

So now, maybe i have to make a payload like this person: https://github.com/fechanique/cordova-plugin-fcm/issues/257

I see the "to":"/topics/all", //Topic or single device and as i do not use topics, will it be the place to put the token ID ? i think not, but how to make the value to match the correct person ?

Baadier-Sydow commented 6 years ago

That is correct. You use the token you received as the "to" value to target that specific device.

Can I ask you to mark this resolved now :)

ghost commented 6 years ago

Ok, thanks a lot, you are realy helpful and kind !

Topic resolved !

ragcsalo commented 6 years ago

My token is still NULL. How can I get a valid token??

anto-christo commented 6 years ago

Same issue as @ragcsalo , token is always NULL. How did you resolve it @Hoplomaque ?

ghost commented 6 years ago

Hello :)

I don't know where your errors are, and i can't help you much.

But i can advise you to take this plugin fork: https://github.com/ostownsville/cordova-plugin-fcm

And the problem might come from your cordova CLI version. Mine was 6.1.1.

All i can do is that, just check your firebase registration in order to avoid smalls errors and check if your getToken() function is in your onDeviceReady() function (or at least the right method).

Hope it could help :)

anto-christo commented 6 years ago

@Hoplomaque , thanks for the reply.