Skip to content

Instantly share code, notes, and snippets.

@sugumura
Last active September 5, 2018 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sugumura/6a5f1ddf9d8f4e28e1514a89d26eefd6 to your computer and use it in GitHub Desktop.
Save sugumura/6a5f1ddf9d8f4e28e1514a89d26eefd6 to your computer and use it in GitHub Desktop.
google/google-auth-library-rubyを利用したFCMへのプッシュ通知サンプル。Ruby on Rails前提
source 'https://rubygems.org'
gem 'rails'
# google/google-auth-library-ruby
gem 'googleauth'
# HTTPリクエストできればなんでもよい
gem 'faraday'
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