FCM宝石使您的红宝石后端通过Firebase Cloud Messaging向Android和iOS设备发送通知。
$ gem install fcm
或在您的Gemfile
中包括它:
gem 'fcm'
对于Android,您将需要一个运行2.3(或更新)的设备,该设备还安装了Google Play商店应用程序,或使用Google API的Android 2.3的模拟器。还支持iOS设备。
当前支持Ruby的版本: ruby >= 2.4
要使用此GEM,您需要使用Firebase凭据实例化客户端:
fcm = FCM . new (
GOOGLE_APPLICATION_CREDENTIALS_PATH ,
FIREBASE_PROJECT_ID
)
GOOGLE_APPLICATION_CREDENTIALS_PATH
GOOGLE_APPLICATION_CREDENTIALS_PATH
旨在包含您的firebase凭据。
提供它们的最简单方法是将这里的绝对途径通过您的凭据:
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
有多个应用程序,则需要唯一命名每个应用程序。这样可以确保通知仅转到预期的目标应用程序。 create
方法将执行此操作,并返回代表设备组的token notification_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 )
您也可以添加/删除某些project_id
的特定notification_key
的注册令牌。例如:
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主题消息使您的应用服务器可以将消息发送到选择到特定主题的多个设备。基于发布/订阅模型,一个应用程序可以订阅不超过2000个主题。发送到主题非常类似于发送到单个设备或用户组,从某种意义上说,您可以使用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
助手:
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
助手:
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!"
}
)
要发送到多个主题的组合,要求您将条件键设置为指定目标主题的布尔条件。例如,将消息发送到订阅主题的设备以及主题b或topicc :
'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)
FCM首先评估括号中的任何条件,然后评估从左到右的表达式。在上面的表达式中,订阅任何单个主题的用户未收到该消息。同样,不订阅主题的用户也不会收到该消息。这些组合确实得到了:
您可以在条件表达式中最多包含五个主题,并支持括号。支持运营商: &&
, ||
, !
。注意!
!('TopicA' in topics)
使用此表达式,任何未订阅主题的应用程序实例,包括未订阅任何主题的应用程序实例,都会收到该消息。
此库中的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 Instance ID Server 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实例ID服务器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客户端应用程序以在此处接收通知的指南:在Android上设置FCM客户端应用程序。
设置iOS应用程序以获取通知的指南在这里:在iOS上设置FCM客户端应用程序。
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 1.0.0
来修复faraday
的折旧警告faraday
替换httparty
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