Skip to content

Instantly share code, notes, and snippets.

@sugumura sugumura/Gemfile
Last active Sep 5, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.