ToothlessGear / node-gcm

A NodeJS wrapper library port to send data to Android devices via Google Cloud Messaging
https://github.com/ToothlessGear/node-gcm
Other
1.3k stars 208 forks source link

Do registration tokens have to be required when sending a notification? #274

Closed junerockwell closed 7 years ago

junerockwell commented 7 years ago

I'm developing a mobile app using Apache Cordova (Ionic). So, I'm using the FCM Cordova plugin. I've only tested on two iOS (iPhone 5S) devices. I am intending to have push notifications to Android soon. I have configured my Apple Push Certificates, provisioning profiles and have uploaded a .p12 to Firebase. For now, I've successfully sent notification messages to the two iPhone 5S devices using this https://cordova-plugin-fcm.appspot.com/. And, both devices received the notification at the same time (which is the intention). I didn't have to do anything to the cordova app except installing the Cordova In-App-Browser plugin and the FCM Cordova plugin.

So this FCM Cordova plugin enables to me to get the token (I'm assuming this is the same registration token in node-gcm documentation/example codes). But I can't find a way for this FCM Cordova Plugin to send this token. My intention is to send the token of the device (once on first startup after installation) to my express-node server that has node-gcm because it seems that registration tokens are required. But if node-gcm doesn't require registration tokens and it will just send the message to FCM and FCM will just send the message to all iOS that have the same App Ids (and Android in the future). So is there a way to not have registration tokens be required when sending the notifications to FCM? Like:

sender.send(message, {} , function (err, response) {
    if (err) console.error(err);
    else console.log(response);
});
junerockwell commented 7 years ago

This problem is solved. I had to dig deeper.

So, I don't have to make all the iOS apps send a device token anywhere. But if I did want to, I'll just have to make an http request to my node server and save it persistently.

In order to send to all iOS apps that have the same provisioning profile, app id, and APN notifications are turned on, all I have to do from my node server is: sender.send(message, { topic: '/topics/all' }, function(err, response) ... { });. The message has to have priority: 'high'.

In the Cordova iOS app, I'll just have to catch the notification using Cordova FCM Plugin; FCMPlugin.onNotification(...);.

I also don't necessarily need to subscribe to the topic. Either, the notification is sent to all the intended iOS apps. But for some reason, after newly installing the app, Xcode 8 console logs out that it can't subscribe to /topics/all or /topics/ios and when I make my node server send the APN, the newly installed app wasn't getting the notification. The other iOS app that has been installed hours before (with the same code) got the notification. I don't know why it's like that. I fixed it by turning off the app and restarting it.

Hopefully, all will be well with the Android part.