Last active
September 5, 2018 08:23
-
-
Save sugumura/6a5f1ddf9d8f4e28e1514a89d26eefd6 to your computer and use it in GitHub Desktop.
google/google-auth-library-rubyを利用したFCMへのプッシュ通知サンプル。Ruby on Rails前提
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'rails' | |
# google/google-auth-library-ruby | |
gem 'googleauth' | |
# HTTPリクエストできればなんでもよい | |
gem 'faraday' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestController < ApplicationController | |
def index | |
scope = 'https://www.googleapis.com/auth/firebase.messaging' | |
authorizer = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: nil, scope: scope) | |
access_token = authorizer.fetch_access_token! | |
# [your-project-name]にFirebaseプロジェクト名 | |
url = "https://fcm.googleapis.com/v1/projects/[your-project-name]/messages:send" | |
request_body = { | |
message: { | |
# topic: "debug", | |
condition: "'debug' in topics && 'news' in topics", | |
notification: { | |
title: "タイトル", | |
body: "本文" | |
}, | |
android: { | |
priority: 'high' | |
}, | |
apns: { | |
headers: { | |
'apns-priority': '10' | |
}, | |
payload: { | |
aps: { | |
sound: 'default' | |
} | |
} | |
} | |
} | |
} | |
conn = Faraday.new(url: url) | |
response = conn.post do |req| | |
req.headers['Content-Type'] = 'application/json' | |
req.headers['Authorization'] = "Bearer " + access_token["access_token"].to_s | |
req.body = request_body.to_json | |
end | |
render :no_content | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment