Skip to content

Instantly share code, notes, and snippets.

@yuki24
Created April 14, 2018 21:13
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 yuki24/029c6bf8ce1cac26b2642662810d66d2 to your computer and use it in GitHub Desktop.
Save yuki24/029c6bf8ce1cac26b2642662810d66d2 to your computer and use it in GitHub Desktop.
Example of the Andpush gem and benchmark
require 'benchmark/ips'
require 'andpush'
require 'fcm'
server_key = ENV.fetch('FCM_TEST_SERVER_KEY')
DEVICE_TOKEN = ENV.fetch('FCM_TEST_REGISTRATION_TOKEN')
FCM_CLIENT = FCM.new(server_key)
PAYLOAD_FOR_FCM = { dry_run: true, notification: { title: "u", body: "y" } }
ANDPUSH_CLIENT = Andpush.build(server_key)
PAYLOAD_FOR_ANDPUSH = PAYLOAD_FOR_FCM.merge(to: DEVICE_TOKEN)
Benchmark.ips do |x|
x.report("andpush") { ANDPUSH_CLIENT.push(PAYLOAD_FOR_ANDPUSH) }
x.report("fcm") { FCM_CLIENT.send(DEVICE_TOKEN, PAYLOAD_FOR_FCM) }
x.compare!
end
require 'andpush'
server_key = "..." # Your server key
device_token = "..." # The device token of the device you'd like to push a message to
client = Andpush.new(server_key)
payload = {
to: device_token,
notification: {
title: "Update",
body: "Your weekly summary is ready"
},
data: {
extra: "data"
}
}
response = client.push(payload)
json = response.json
json[:canonical_ids] # => 0
json[:failure] # => 0
json[:multicast_id] # => 8478364278516813477
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment