rails/action_push_native

Instructions for `push.yml` config are not clear enough

Closed this issue · 6 comments

I've been trying to use this gem with google FCM, but I can't get it to initialize. In the readme example, you have encryption_key: your_service_account_json_file, which I assume is instructing me to point to a json file. I downloaded the file from Firebase account console, as instructed in the linked docs. But no matter how I try, it always gives me yaml parsing errors.

the file I got from firebase is like this:

{
  "type": "service_account",
  "project_id": "my-project-abc123",
  "private_key_id": "some_key",
  "private_key": "-----BEGIN PRIVATE KEY-----
long_chars_sequence
-----END PRIVATE KEY-----\n",
  "client_email": "firebase-adminsdk-fbsvc@my-project-abc123.iam.gserviceaccount.com",
  "client_id": "123456",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-fbsvc%40my-project-abc123.iam.gserviceaccount.com",
  "universe_domain": "googleapis.com"
}

I saved it as config/firebase-credentials.json

Here is my push.yml file:

shared:
  google:
    # Your Firebase project service account credentials
    # See https://firebase.google.com/docs/cloud-messaging/auth-server
    encryption_key: "<%= File.read(Rails.root.join('config/firebase-credentials.json')) %>"

    # Firebase project_id
    project_id: my-project-abc123

    # Change the request timeout (default: 15).
    # request_timeout: 30

Here is the error I get when trying to send the notification:

`<main>': YAML syntax error occurred while parsing /Users/Fabiano/Dropbox/code/my_rails_app/config/push.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Error: (<unknown>): did not find expected key while parsing a block mapping at line 2 column 3 (RuntimeError)
/Users/Fabiano/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/psych-5.2.6/lib/psych/parser.rb:62:in `_native_parse': (<unknown>): did not find expected key while parsing a block mapping at line 2 column 3 (Psych::SyntaxError)

Am I doing something wrong?

rosa commented

Hey @fabianoarruda, could you try using dump on the string resulting from the file read? Like this:

encryption_key: "<%= File.read(Rails.root.join('config/firebase-credentials.json')).dump %>"

There is also a spacing issue in the YAML, please try following this format in the encrypted credentials file, or push.yml if you are just testing it:

  private_key: |
    -----BEGIN PRIVATE KEY-----
    ALLTHEPRIVATEKEY
    GOES
    HERE
    -----END PRIVATE KEY-----

Let us know if that fixes it. I’ll also look into making the README clearer.

That YAML spacing fix from @intrip is probably best, but FWIW you can also make it a one-liner w/ newline special chars:

private_key: "-----BEGIN PRIVATE KEY-----\nXXX\nXXX\nXXX\nXXX\n-----END PRIVATE KEY-----"

I updated the Readme suggesting to use the encrypted credentials. Closing this for now, but FF to reopen if something is still unclear.

I just wanted to note that using newline special chars in credentials.yml required .dump in push.yml like so:

encryption_key: <%= Rails.application.credentials.services.apns.private_key.dump %>

Thanks @trevorturk! Handled in #47