parse-community / parse-server-push-adapter

A push notification adapter for Parse Server
https://parseplatform.org
MIT License
87 stars 99 forks source link

FCM adapter #32

Closed thailey01 closed 7 years ago

thailey01 commented 7 years ago

How would we go about developing a push adapter for FCM. Projects created in Google Console seem to now be required to use Firebase Cloud Messaging instead of GCM. I've tried to start an FCM adapter by copying the GCM adapter where I could but I'm changing certain parts when needed based on fcm-node documentation.

flovilmart commented 7 years ago

We could add a new handler here for fcm, so it processes properly fcm based push alongside GCM. The FCM compatible installations will have pushType set to fcm instead of gcm. What do you think?

thailey01 commented 7 years ago

That'd be perfect! Wish I could help in some way.

flovilmart commented 7 years ago

No need for the FCM adapter, just initialize with the right key and the GCM adapter pushes will automagically send everything correctly!

thailey01 commented 7 years ago

Don't you need a serverapi key and a project number? I don't think firebase supplies the project number or key.

thailey01 commented 7 years ago

Nevermind, Firebase creates a senderId and Server Api Key which can be found in the project settings.

thailey01 commented 7 years ago

Do we still have to add firebase to our projects? I set the senderId and apiKey found in Firebase but the message wasn't pushed.

flovilmart commented 7 years ago

Does the token was acquired with the firebase SDK? Not sure about the compatibility here

thailey01 commented 7 years ago

When setting up fcm, the Firebase console automatically creates a senderId and a serverApiKey.

flovilmart commented 7 years ago

I means the android device tokens. Maybe we should still let gcm and fcm token live separately.

thailey01 commented 7 years ago

Oh, I haven't added the Firebase sdk to my app so the token must be generated by parse. Basically, I'm running parse-server as usual but have setup fcm (which created a serverkey and senderId). I'm then placing that key and id in parse-server just like I would with gcm key and id. Sorry for the confusion.

flovilmart commented 7 years ago

All device tokens acquired with GCM should work with FCM. But if you changed your senderID and serverKey, that's likely to be problematic IMHO yet I'm not sure

thailey01 commented 7 years ago

The thing is I haven't setup gcm with parse-server. I was using push through hosted parse but it didn't require me to create an app for gcm. Now the only option we have is to add fcm instead of gcm and it doesn't seem to be working. I setup my parse app (and server side) just like how you would with gcm and added the fcm credentials to it, which is really only the server key but that shouldn't be any different.

flovilmart commented 7 years ago

@inlined may have more input on that.

thailey01 commented 7 years ago

Ok, I think I've figured it out finally. There is a little more setup, however when adding fcm to your android project (if you haven't already setup gcm) you can keep your parse push configuration the same in the androidmanifest.xml but you have to add the firebase library to your project. This allows receiving message and customizing receiver service because fcm message doesn't show up when app is in foreground. I followed this page and got it working https://firebase.google.com/docs/notifications/android/console-audience

thailey01 commented 7 years ago

I seem to be able to receive notifications but sending them doesn't work.

thailey01 commented 7 years ago

Nevermind, I had forgotten the meta-data for gcm_sender_id at the bottom of manifest. Everything has remained the same as far as parse push.

Onwa1kenobi commented 7 years ago

@thailey01 by "Adding Firebase your project", do you mean just adding the import in gradle or did you also implement the FirebaseMessagingService? Also, what library did you import, the core, messaging or both?

thailey01 commented 7 years ago

In the end, I found out that you don't even need to import any library while using parse-server. I was missing the meta-data tag in my manifest and everything worked fine.

kiis commented 7 years ago

Thailey were you using device->device pushes or device->parse-server->device pushes ? This solution here does not make any sense based on current adapter. Seems you were not using adapter at all to send and recieve pushes. So changing adapter to support FCM is still valid issue, because it uses npm-gcm, but should use npm-fcm library for new users.

thailey01 commented 7 years ago

As I'm using parse-server I am calling a cloud function from my native app to send push notifications. The adapter as it is now seems to support fcm just the same as gcm. I changed absolutely nothing from the current documentation for setting up parse-server push.

kiis commented 7 years ago

What values do you use in parse-server config file ?

I have these fields and it seems not to work with default adapter.

{"android": { "senderId": "FCM senderId", "apiKey": "FCM server API key" },

thailey01 commented 7 years ago

Those are the same I use, found from the firebase console settings page. Be sure you have added the meta tag in your manifest to set the gcm key just as you normally would. That was my problem.

On Sep 13, 2016 5:34 PM, "kiis" notifications@github.com wrote:

What values do you use in parse-server config file ?

I have these fields and it seems not to work with default adapter.

{"android": { "senderId": "FCM senderId", "apiKey": "FCM server API key" },

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/parse-server-modules/parse-server-push-adapter/issues/32#issuecomment-246847537, or mute the thread https://github.com/notifications/unsubscribe-auth/AFkyEhjynFy2rMC7BBWtrMlBrTZtk25Uks5qpyUIgaJpZM4J0i1o .

kiis commented 7 years ago

ok, server side seems the same, manifest has meta tag. Still shows that android pushes are not delivered. In database installation table do you have deviceToken or GCMSenderId for android users ?

thailey01 commented 7 years ago

Device token

thailey01 commented 7 years ago

could you show your index.js and manifest file?

thailey01 commented 7 years ago

In addition, I don't know if you actually have it that way, but I don't put quotes around my keys in my config. Such as: {android: { senderId: "FCM senderId", apiKey: "FCM server API key" },

kiis commented 7 years ago

Manifest: http://pastebin.com/FAcCrAkC Server config: http://pastebin.com/j3VmTBJ2

Server is using pm2 and it has config differently listed, but it works and ios pushes also.

thailey01 commented 7 years ago

The only difference I see between your manifest and mine is that I specify ParseBroadcastReceiver `

`

kristokiis commented 7 years ago

added it, but there is still null value in isntallation table for field deviceToken

Onwa1kenobi commented 7 years ago

I was also getting null value returned for the deviceToken. My case was terrible, sometimes, it returns null, sometimes, it returns a value. what i did was to clean the project as instant run in android studio is quite buggy.

thailey01 commented 7 years ago

Are you calling ParseInstallation.getCurrentInstallation().saveInBackground() when you setup parse-server config In your app? I do mine in a class that extends Application.

kiis commented 7 years ago

Found the issue. Adapter works perfectly. The issue is java.lang.Exception: GCM registration error: INVALID_SENDER . Seems FCM SenderId is not accepted in GCM server by Android SDK. Now im manually copying GCMSenderId to deviceToken field afterSave in cloud code.

thailey01 commented 7 years ago

That a little bit of a hassle, but I'm glad you figured out the problem. I don't know why I didn't have it though.

mmazzarolo commented 7 years ago

Disclaimer: This is the first time I'm implementing push notifications.

Hey there! Is there any way to use FCM in parse-server-push-adapter for sending notifications on both android and iOS devices?
I'm still trying to grok the concept, but from my understanding it should be possible to use parse-server for sending a push through FCM independently from the device type, am I right?

Thanks guys :)

edit: I just tried using the master branch. It works fine on Android, but not on iOS. Setting the pushType to android on an iOS device redirect correctly to the GCM and I get a successful response:

verb parse-server-push-adapter GCM GCM Response: {
verb parse-server-push-adapter GCM     "multicast_id": 6094255852378874000,
verb parse-server-push-adapter GCM     "success": 2,
verb parse-server-push-adapter GCM     "failure": 0,
verb parse-server-push-adapter GCM     "canonical_ids": 0,
verb parse-server-push-adapter GCM     "results": [
verb parse-server-push-adapter GCM         {
verb parse-server-push-adapter GCM             "message_id": "0:1477468345295055%99573f33f9fd7ecd"
verb parse-server-push-adapter GCM         },
verb parse-server-push-adapter GCM         {
verb parse-server-push-adapter GCM             "message_id": "0:1477468345198750%99573f33f9fd7ecd"
verb parse-server-push-adapter GCM         }
verb parse-server-push-adapter GCM     ]
verb parse-server-push-adapter GCM }

Still, on the device I don't receive any notification (sending the push through the firebase console works though).
I'd like to help on this feature but as I already said I have no experience on push notifications... do you have any suggestions?
@flovilmart @thailey01 @jeffjen

Thank you guys, let me know if I can help in any way :)

felimartina commented 7 years ago

We are creating an application using parse-server. We created a firebase account in order to send push notifications to our android devices. However, everytime we send a Push Notification to an Android device the device does not display the notification. It does arrive, though. The Android dev states that the notification is missing the notification field which Android uses in order to SHOW the notification in the screen. Are we missing anything? Again, notification arrive and can be handled, however, it is not automatically shown by Android on the screen.

This is the body of the notification we are sending:

{
    "where" : {
        "user" : {
            "__type" : "Pointer",
            "className" : "_User",
            "objectId" : "RKGGWkXaIh"
        }
    },
    "data" : {
        "body" : "great match!",
        "title" : "Portugal vs. Denmark"
    }
}

I read about it in multiple places but seems like we need to have a FCM adapter that knows how to handle FCM push notifications. I debugged the GCM push adapter and does not have any clue or whatsoever of what a notification field is. I checked the fcm-adapter branch in this repo (the only open branch other than master) and that one seems to have an FCM adapter which DOES TAKE INTO ACCOUNT notification field. I believe I should be able to request a notification with the following body and would work using the fcm-adapter branch.

{
    "where" : {
        "user" : {
            "__type" : "Pointer",
            "className" : "_User",
            "objectId" : "RKGGWkXaIh"
        }
    },
    "data" : {
        "body" : "great match!",
        "title" : "Portugal vs. Denmark",
        "notification":{
          "body" : "great match!",
          "title" : "Portugal vs. Denmark"
        }
    }
}

any thoughts? what shall we do? Is there a way to continue to use parse-server and get notifications sent AND DISPLAYED to Android devices? Do I need to tell the Android dev to change something in the app settings??? where? what?

Any help would be appreciated.

Thanks in advance

flovilmart commented 7 years ago

As you mention, the current adapter works with FCM, just the notification field is missing. Do you want to make a pull request to support that?

felimartina commented 7 years ago

But without notification field nothing is displayed on the Android device. What's the point of having notifications working but not having them displayed on the screen? I pulled and compiled fcm-adapter branch and seems to be working fine if you set the device's pushType equal to fcm (also need to add fcm to parse push notifications). Not sure how stable the fcm-adapter branch is though.

flovilmart commented 7 years ago

There is no need to use the fcm branch, the master branch should work correctly with fcm

felimartina commented 7 years ago

Mhhh...could you explain me a bit further? If I use master branch, what do I need to change on the Android App (or in my server when requesting a push notification) in order to get Push Notifications DISPLAYED on the phone screen? This is how I am requesting the push notification and it is not being displayed on the Android screen. Am I missing something?:

curl -X POST \
  -H "X-Parse-Application-Id: ${APPLICATION_ID}" \
  -H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
        "where" : {
        "user" : {
            "__type" : "Pointer",
            "className" : "_User",
            "objectId" : "RKGGWkXaIh"
        }
    },
        "data" : {
        "body" : "great match!",
        "title" : "Portugal vs. Denmark",
        "alert": "something to say"
    }
      }' \
  http://localhost:4040/parse/push

FYI...I am using parse-server-push-adapter from npm (1.1.0). Is that the very same version that holds the master branch?

flovilmart commented 7 years ago

See the node-gcm documentation here: https://www.npmjs.com/package/node-gcm

The notification key is not set when creating the gcm.message here:

https://github.com/parse-server-modules/parse-server-push-adapter/blob/master/src/GCM.js#L124

and here:

https://github.com/parse-server-modules/parse-server-push-adapter/blob/master/src/GCM.js#L60

felimartina commented 7 years ago

That is what I mean. Let me re-phrase. How do I do in order to DISPLAY a notification on an Android phone?

flovilmart commented 7 years ago

One need to open a pull request, follow the examples from the node-gcm package and implement the notification key inside the GCM payload.

felimartina commented 7 years ago

Okay, Just like someone did in the fcm-adapter. The thing is that there is no such feature in parse then, correct? There is no way, currently, to display android notifications in the phone screen using parse-server.

BTW...thank you so much for your quick responses @flovilmart and for helping me to troubleshoot this

flovilmart commented 7 years ago

I believe this is a change in the google API's and we didn't catch it up. Because you caught it, that would be nice if you provide the fix too.

felimartina commented 7 years ago

Awesome! It just feels weird I am the only one seeing this issue (thought multiple people are using parse for sending push notifications). I will try to put something together to overcome this problem. But I am only seeing this problem when using fcm setttigs and the gcm sender from parse. I will try to create a pull request (need to catch up on the repo rules first)... I see the fcm-adapter is doing kind of the same here. Any other thought on your side @flovilmart ?

Again, thanks for everything

flovilmart commented 7 years ago

The fcm branch is abandoned and should be deleted, as I thought that fcm was not compatible with the node-gcm package. You can get some inspiration from there definitely.

mmazzarolo commented 7 years ago

thought multiple people are using parse for sending push notifications

@felimartina I had the same issue but I thought it was just a mis-configuration on my side

felimartina commented 7 years ago

@mmazzarolo will see if I grab some spare time these days and create a pull request...should be as simple as taking into account notification field when sending the GCM in the GCM.js file. Let me know if you are experiencing some other issue we can see if we can add support for other options that can be sent to Android phones. These are the fields supported in the gmc/fmc payload:

https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support

mmazzarolo commented 7 years ago

@felimartina https://github.com/parse-server-modules/parse-server-push-adapter/pull/41

jamlfy commented 7 years ago

Well 'GCM', is oldddd.....

https://developers.google.com/cloud-messaging/