อัญมณี FCM ช่วยให้แบ็กเอนด์ทับทิมของคุณส่งการแจ้งเตือนไปยังอุปกรณ์ Android และ iOS ผ่านการส่งข้อความคลาวด์ Firebase
$ gem install fcm
หรือใน Gemfile
ของคุณรวมไว้ด้วย:
gem 'fcm'
สำหรับ Android คุณจะต้องใช้อุปกรณ์ที่ใช้งาน 2.3 (หรือใหม่กว่า) ที่ติดตั้งแอป Google Play Store หรือตัวจำลองที่ใช้ Android 2.3 กับ Google APIs อุปกรณ์ iOS ยังรองรับ
Ruby ที่รองรับรุ่นปัจจุบัน: ruby >= 2.4
ในการใช้อัญมณีนี้คุณจะต้องสร้างอินสแตนซ์ลูกค้าด้วยข้อมูลรับรอง Firebase ของคุณ:
fcm = FCM . new (
GOOGLE_APPLICATION_CREDENTIALS_PATH ,
FIREBASE_PROJECT_ID
)
GOOGLE_APPLICATION_CREDENTIALS_PATH
GOOGLE_APPLICATION_CREDENTIALS_PATH
นั้นมีไว้เพื่อให้มีข้อมูลประจำตัวของคุณ
วิธีที่ง่ายที่สุดในการให้พวกเขาคือการผ่านเส้นทางที่แน่นอนไปยังไฟล์ที่มีข้อมูลประจำตัวของคุณ:
fcm = FCM . new (
'/path/to/credentials.json' ,
FIREBASE_PROJECT_ID
)
ตามธรรมชาติที่เป็นความลับของพวกเขาคุณอาจไม่ต้องการให้พวกเขาอยู่ในที่เก็บของคุณ ในกรณีนี้โซลูชันที่รองรับอื่นคือการผ่าน StringIO
ที่มีข้อมูลประจำตัวของคุณ:
fcm = FCM . new (
StringIO . new ( ENV . fetch ( 'FIREBASE_CREDENTIALS' ) ) ,
FIREBASE_PROJECT_ID
)
เพื่อย้ายไปยัง http v1 ดู: https://firebase.google.com/docs/cloud-messaging/migrate-v1
fcm = FCM . new (
GOOGLE_APPLICATION_CREDENTIALS_PATH ,
FIREBASE_PROJECT_ID
)
message = {
'token' : "000iddqd" , # send to a specific device
# 'topic': "yourTopic",
# 'condition': "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)",
'data' : {
payload : {
data : {
id : 1
}
} . to_json
} ,
'notification' : {
title : notification . title_th ,
body : notification . body_th ,
} ,
'android' : { } ,
'apns' : {
payload : {
aps : {
sound : "default" ,
category : " #{ Time . zone . now . to_i } "
}
}
} ,
'fcm_options' : {
analytics_label : 'Label'
}
}
fcm . send_v1 ( message ) # or fcm.send_notification_v1(message)
ด้วยการส่งข้อความกลุ่มอุปกรณ์คุณสามารถส่งข้อความเดียวไปยังหลายอินสแตนซ์ของแอพที่ทำงานบนอุปกรณ์ที่เป็นของกลุ่ม โดยทั่วไปแล้ว "กลุ่ม" หมายถึงชุดของอุปกรณ์ต่าง ๆ ที่เป็นของผู้ใช้รายเดียว อย่างไรก็ตามกลุ่มยังสามารถเป็นตัวแทนชุดของอุปกรณ์ที่อินสแตนซ์ของแอพฟังก์ชั่นในลักษณะที่มีความสัมพันธ์สูง ในการใช้คุณสมบัตินี้คุณจะต้องใช้คลาส FCM
ที่เริ่มต้นก่อน
จำนวนสมาชิกสูงสุดที่อนุญาตสำหรับคีย์การแจ้งเตือนคือ 20 https://firebase.google.com/docs/cloud-messaging/android/device-group#managing_device_groups
จากนั้นคุณจะต้องมีคีย์การแจ้งเตือนซึ่งคุณสามารถสร้างสำหรับ key_name
เฉพาะซึ่งจะต้องมีชื่อเฉพาะต่อแอปในกรณีที่คุณมีแอพหลายตัวสำหรับ project_id
เดียวกัน สิ่งนี้ทำให้มั่นใจได้ว่าการแจ้งเตือนไปที่แอพ Target ที่ตั้งใจไว้เท่านั้น วิธี create
จะทำสิ่งนี้และส่งคืนโทเค็น notification_key
_key ซึ่งแสดงถึงกลุ่มอุปกรณ์ในการตอบสนอง:
project_id
คือ Sender_ID ในการตั้งค่าคลาวด์ของคุณ https://firebase.google.com/docs/cloud-messaging/concept-options#senderid
params = { key_name : "appUser-Chris" ,
project_id : "my_project_id" ,
registration_ids : [ "4" , "8" , "15" , "16" , "23" , "42" ] }
response = fcm . create ( * params . values )
ในการส่งข้อความไปยังกลุ่มอุปกรณ์ให้ใช้ HTTP V1 API การส่งข้อความไปยังกลุ่มอุปกรณ์นั้นคล้ายคลึงกับการส่งข้อความไปยังอุปกรณ์แต่ละตัวโดยใช้วิธีเดียวกันเพื่ออนุมัติคำขอส่ง ตั้งค่าฟิลด์โทเค็นเป็นคีย์การแจ้งเตือนกลุ่ม
message = {
'token' : "NOTIFICATION_KEY" , # send to a device group
# ...data
}
fcm . send_v1 ( message )
นอกจากนี้คุณยังสามารถเพิ่ม/ลบโทเค็นการลงทะเบียนไปที่/จาก notification_key
เฉพาะ _Key ของ project_id
ตัวอย่างเช่น:
params = { key_name : "appUser-Chris" ,
project_id : "my_project_id" ,
notification_key : "appUser-Chris-key" ,
registration_ids : [ "7" , "3" ] }
response = fcm . add ( * params . values )
params = { key_name : "appUser-Chris" ,
project_id : "my_project_id" ,
notification_key : "appUser-Chris-key" ,
registration_ids : [ "8" , "15" ] }
response = fcm . remove ( * params . values )
การส่งข้อความหัวข้อ FCM ช่วยให้แอพเซิร์ฟเวอร์ของคุณส่งข้อความไปยังอุปกรณ์หลายตัวที่เลือกใช้ในหัวข้อเฉพาะ ขึ้นอยู่กับรูปแบบการเผยแพร่/สมัครสมาชิกอินสแตนซ์หนึ่งแอพสามารถสมัครได้ไม่เกิน 2,000 หัวข้อ การส่งไปยังหัวข้อนั้นคล้ายกับการส่งไปยังอุปกรณ์แต่ละตัวหรือไปยังกลุ่มผู้ใช้ในแง่ที่ว่าคุณสามารถใช้วิธี fcm.send_v1
ที่ topic
ตรงกับนิพจน์ทั่วไป "/topics/[a-zA-Z0-9-_.~%]+"
:
message = {
'topic' : "yourTopic" , # send to a device group
# ...data
}
fcm . send_v1 ( message )
หรือคุณสามารถใช้ fcm.send_to_topic
HELPER:
response = fcm . send_to_topic ( "yourTopic" ,
notification : { body : "This is a FCM Topic Message!" } )
การส่งข้อความเงื่อนไขหัวข้อ FCM เพื่อส่งข้อความไปยังการรวมกันของหัวข้อระบุเงื่อนไขซึ่งเป็นนิพจน์บูลีนที่ระบุหัวข้อเป้าหมาย
message = {
'condition' : "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)" , # send to topic condition
# ...data
}
fcm . send_v1 ( message )
หรือคุณสามารถใช้ fcm.send_to_topic_condition
HELPER:
response = fcm . send_to_topic_condition (
"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)" ,
notification : {
body : "This is an FCM Topic Message sent to a condition!"
}
)
ในการส่งไปยังการรวมกันของหลายหัวข้อต้องให้คุณตั้งค่าคีย์ เงื่อนไข ให้เป็นเงื่อนไขบูลีนที่ระบุหัวข้อเป้าหมาย ตัวอย่างเช่นในการส่งข้อความไปยังอุปกรณ์ที่สมัครเป็นสมาชิก Topica และ TopicB หรือ TOBICC :
'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)
FCM ประเมินเงื่อนไขใด ๆ ในวงเล็บแล้วประเมินการแสดงออกจากซ้ายไปขวา ในนิพจน์ข้างต้นผู้ใช้ที่สมัครรับหัวข้อเดียวไม่ได้รับข้อความ ในทำนองเดียวกันผู้ใช้ที่ไม่ได้สมัครเป็นสมาชิก Topica ไม่ได้รับข้อความ ชุดค่าผสมเหล่านี้ได้รับ:
คุณสามารถรวมหัวข้อได้สูงสุดห้าหัวข้อในการแสดงออกตามเงื่อนไขของคุณและรองรับวงเล็บ ผู้ให้บริการที่รองรับ: &&
, ||
, !
- สังเกตการใช้งาน!:
!('TopicA' in topics)
ด้วยนิพจน์นี้อินสแตนซ์แอพใด ๆ ที่ไม่ได้สมัครเป็นสมาชิก TOBICA รวมถึงอินสแตนซ์แอพที่ไม่ได้สมัครเป็นสมาชิกหัวข้อใด ๆ รับข้อความ
วิธี send_to_topic_condition
ภายในไลบรารีนี้ช่วยให้คุณสามารถแยกแยะเงื่อนไขของหลายหัวข้อที่จะส่งไปยังข้อมูลข้อมูล
response = fcm . send_to_topic_condition (
"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)" ,
notification : {
body : "This is an FCM Topic Message sent to a condition!"
}
)
ด้วยโทเค็นการลงทะเบียนและชื่อหัวข้อคุณสามารถเพิ่มโทเค็นลงในหัวข้อโดยใช้ Google Instance ID Server API
topic = "YourTopic"
registration_token = "12" # a client registration token
response = fcm . topic_subscription ( topic , registration_token )
# or unsubscription
response = fcm . topic_unsubscription ( topic , registration_token )
หรือคุณสามารถจัดการแผนที่ความสัมพันธ์สำหรับอินสแตนซ์แอปหลายอินสแตนซ์ Google อินสแตนซ์เซิร์ฟเวอร์ API จัดการความสัมพันธ์
topic = "YourTopic"
registration_tokens = [ "4" , "8" , "15" , "16" , "23" , "42" ] # an array of one or more client registration tokens
response = fcm . batch_topic_subscription ( topic , registration_tokens )
# or unsubscription
response = fcm . batch_topic_unsubscription ( topic , registration_tokens )
เมื่อได้รับโทเค็นการลงทะเบียนคุณสามารถดึงข้อมูลเกี่ยวกับโทเค็นโดยใช้ Google Instance ID Server API
registration_token = "12" # a client registration token
response = fcm . get_instance_id_info ( registration_token )
ในการรับข้อมูลรายละเอียดเกี่ยวกับ ID อินสแตนซ์คุณสามารถผ่าน options
เลือกแฮชไปยังวิธี get_instance_id_info
:
registration_token = "12" # a client registration token
options = { "details" => true }
response = fcm . get_instance_id_info ( registration_token , options )
คุณสามารถค้นหาคำแนะนำในการใช้แอพไคลเอนต์ Android เพื่อรับการแจ้งเตือนที่นี่: ตั้งค่าแอพไคลเอนต์ FCM บน Android
คำแนะนำในการตั้งค่าแอพ iOS เพื่อรับการแจ้งเตือนอยู่ที่นี่: การตั้งค่าแอปไคลเอนต์ FCM บน iOS
http_options
เพื่อ initialize
เมธอดและตัวเลือก timeout
แบบขาวAPI_KEY
ที่เลิกใช้แล้วsend
ที่เลิกใช้แล้วsend_with_notification_key
ที่เลิกใช้แล้วsubscribe_instance_id_to_topic
unsubscribe_instance_id_from_topic
batch_subscribe_instance_ids_to_topic
วิธีการbatch_unsubscribe_instance_ids_from_topic
วิธีการ send_to_topic_condition
send_to_topic
Google::Auth::ServiceAccountCredentials
#103faraday
จาก 1 ถึง 2 #101DEFAULT_TIMEOUT
ไปยัง faraday
#96get_instance_id_info
ตัวเลือกพารามิเตอร์ #98ขอบคุณมากสำหรับ @excid3 @jsparling @jensljungblad
>= 2.4
faraday
โดยการเปลี่ยนเวอร์ชันการพึ่งพาเป็น faraday 1.0.0
httparty
ด้วย faraday
recover_notification_key
อัปเดตเวอร์ชันใน fcm.gemspec
พร้อม VERSION
และอัปเดตส่วน README.md
## ChangeLog
# set the version
# VERSION="1.0.7"
gem build fcm.gemspec
git tag -a v ${VERSION} -m " Releasing version v ${VERSION} "
git push origin --tags
gem push fcm- ${VERSION} .gem