Rails - Heroku - Push notifications work via Postman but not via my Rails API

Viewed 280

I want to send push notifications to devices using expo. I am successfully getting expo_push_token for each device that has enabled access and I am also successfully able to make the request using Postman. Here is my Postman request: POST request to: https://exp.host/--/api/v2/push/send

{
 "to": "ExponentPushToken[xxx]",
 "title": "yesahhhh",
 "body": "lkajdsljaslda"
}

Headers include:

Accept: application/json
Content-type: application/json
Accept encoding: gzip, deflate
Host: exp.host

So, the above work as it should via postman.

However, I cannot get it to work when I run the Heroku command: heroku run rake run_job

The run_job method is referenced in my rails app in 2 places:

lib/task/scheduler.rake:

task :run_job => :environment do
    puts "Running Job..."
    User.run_job
    puts "done!" 
end

user.rb:

  def self.run_job
    User.all.each do |u|
      if u.expo_push_token.present?
        require 'net/http'
        require 'json'
        begin
          uri = URI('https://exp.host/--/api/v2/push/send')
          http = Net::HTTP.new(uri.host, uri.port)
          http.use_ssl = true

          request = Net::HTTP::Post.new(uri.path, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
          request.body = {to: u.expo_push_token, title: "Prana", body: "Time to affirm and visualize"}.to_json

          response = http.request(request)
          puts "The Response: #{response.body}"
        rescue => e
          puts "failed #{e}"
        end
      end
    end
  end

The user successfully receives the expo_push_token, however the push notification is not received. When I run the command heroku run rake run_job this is the response I get on terminal:

Running rake run_job on ⬢ prana-app... up, run.7732 (Free)
Running Job...
D, [2019-07-01T19:17:31.215548 #4] DEBUG -- :   User Load (2.1ms)  SELECT "users".* FROM "users"
done!

What is missing in my ruby code thats preventing the push notifications from being received?

0 Answers
Related