decision-labs / fcm

Ruby bindings to Firebase Cloud Messaging (FCM) for Android, iOS or Web
MIT License
517 stars 154 forks source link

Error authenticating the sender account via controller action #95

Closed matbrady closed 2 years ago

matbrady commented 2 years ago

API Version

Legacy HTTP

Problem

From the app server I am able to:

  1. Validate the Server Key by performing the curl request described here (https://firebase.google.com/docs/cloud-messaging/auth-server#checkAPIkey)
  2. Successfully create a push notification that is delivered to a real device. This is done manually via the rails console. Example code:
FcmHelper.send_notification_to_users(['$DEVICE_TOKEN'], 'Title', 'body')
Full Method ```ruby def self.send_notification_to_users(tokens, title, body, data = {}) fcm = FCM.new( ENV["FIREBASE_KEY"] ) message = { 'notification': { title: title, body: body, }, } response = fcm.send(tokens, message) end ```

produces a successful 200 response:

{:body=>"{\"multicast_id\":8133696875296849273,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"1641999895166419\"}]}", :headers=>{"content-type"=>"application/json; charset=UTF-8", "date"=>"Wed, 12 Jan 2022 15:04:55 GMT", "expires"=>"Wed, 12 Jan 2022 15:04:55 GMT", "cache-control"=>"private, max-age=0", "x-content-type-options"=>"nosniff", "x-frame-options"=>"SAMEORIGIN", "content-security-policy"=>"frame-ancestors 'self'", "x-xss-protection"=>"1; mode=block", "server"=>"GSE", "alt-svc"=>"h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\"", "connection"=>"close", "transfer-encoding"=>"chunked"}, :status_code=>200, :response=>"success", :canonical_ids=>[], :not_registered_ids=>[]} 

However, running the same FcmHelper.send_notification_to_users method above within a controller action produces a 401 authentication error:

"status_code":401,"response":"There was an error authenticating the sender account."
Full response ``` {"body":"\u003cHTML\u003e\n\u003cHEAD\u003e\n\u003cTITLE\u003eINVALID_KEY\u003c/TITLE\u003e\n\u003c/HEAD\u003e\n\u003cBODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\"\u003e\n\u003cH1\u003eINVALID_KEY\u003c/H1\u003e\n\u003cH2\u003eError 401\u003c/H2\u003e\n\u003c/BODY\u003e\n\u003c/HTML\u003e\n","headers":{"content-type":"text/html; charset=UTF-8","date":"Wed, 12 Jan 2022 15:14:17 GMT","expires":"Wed, 12 Jan 2022 15:14:17 GMT","cache-control":"private, max-age=0","x-content-type-options":"nosniff","x-frame-options":"SAMEORIGIN","content-security-policy":"frame-ancestors 'self'","x-xss-protection":"1; mode=block","server":"GSE","alt-svc":"h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\"","connection":"close","transfer-encoding":"chunked"},"status_code":401,"response":"There was an error authenticating the sender account." ```

Question

Any thoughts on why messages would be sent successfully via the console but have an authentication error from within controller logic?

sabman commented 2 years ago

@matbrady looking at the response seems like something isn't right with the FIREBASE_KEY can you view your logs to verify that when you start the rails server the environment variables are loaded? You can just try to print the ENV["FIREBASE_KEY"] to your logs in the controller.

sabman commented 2 years ago

Closing since it looks like an environment configuration issue for the user. @matbrady feel free to reopen if you find its a problem in the gem