Twisted Python客户端用于FCM - Firebase云消息(Android和iOS)。
项目描述
Twisted Python客户端用于FCM - Firebase云消息(Android和iOS)
Firebase云消息(FCM)是GCM的新版本。它继承了可靠和可扩展的GCM基础设施,并增加了新功能。强烈建议GCM用户升级到FCM。
使用FCM,您可以通知客户端应用程序新的电子邮件或其他数据可供同步。您可以向用户发送通知以驱动用户重新参与和保留。对于即时消息等用例,一条消息可以将最多4KB的有效负载传输到客户端应用程序。
有关更多信息,请访问:https://firebase.google.com/docs/cloud-messaging/
链接
快速入门
使用pip安装
pip install txfcm OR pip install git+https://github.com/linteltechnologies/txfcm.git
txfcm支持Android和iOS。
功能
涵盖所有FCM功能
示例
使用TXFCMNotification类发送通知
# Send to single device.
from txfcm import TXFCMNotification
from twisted.internet import reactor
push_service = TXFCMNotification(api_key="<api-key>")
# Your api-key can be gotten from: https://console.firebase.google.com/project/<project-name>/settings/cloudmessaging
registration_id = "<device registration_id>"
message_title = "Uber update"
message_body = "Hi john, your customized news for today is ready"
result = push_service.notify_single_device(registration_id=registration_id, message_title=message_title, message_body=message_body)
# Send to multiple devices by passing a list of ids.
registration_ids = ["<device registration_id 1>", "<device registration_id 2>", ...]
message_title = "Uber update"
message_body = "Hope you're having fun this weekend, don't forget to check today's news"
df = push_service.notify_multiple_devices(registration_ids=registration_ids, message_title=message_title, message_body=message_body)
def got_result(result):
print result
df.addBoth(got_result)
reactor.run()
发送数据消息。
# With FCM, you can send two types of messages to clients:
# 1. Notification messages, sometimes thought of as "display messages."
# 2. Data messages, which are handled by the client app.
# Client app is responsible for processing data messages. Data messages have only custom key-value pairs. (Python dict)
# Data messages let developers send up to 4KB of custom key-value pairs.
# Sending a notification with data message payload
data_message = {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
# To multiple devices
result = push_service.notify_multiple_devices(registration_ids=registration_ids, message_body=message_body, data_message=data_message)
# To a single device
result = push_service.notify_single_device(registration_id=registration_id, message_body=message_body, data_message=data_message)
# Sending a data message only payload, do NOT include message_body
# To multiple devices
result = push_service.notify_multiple_devices(registration_ids=registration_ids, data_message=data_message)
# To a single device
result = push_service.notify_single_device(registration_id=registration_id, data_message=data_message)
# Use notification messages when you want FCM to handle displaying a notification on your app's behalf.
# Use data messages when you just want to process the messages only in your app.
# txfcm can send a message including both notification and data payloads.
# In such cases, FCM handles displaying the notification payload, and the client app handles the data payload.
发送低优先级消息。
# The default is low_priority == False
result = push_service.notify_multiple_devices(registration_ids=registration_ids, message_body=message, low_priority=True)
向主题发送消息。
# Send a message to devices subscribed to a topic.
result = push_service.notify_topic_subscribers(topic_name="news", message_body=message)
# Conditional topic messaging
topic_condition = "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"
result = push_service.notify_topic_subscribers(message_body=message, condition=topic_condition)
# FCM first evaluates any conditions in parentheses, and then evaluates the expression from left to right.
# In the above expression, a user subscribed to any single topic does not receive the message. Likewise,
# a user who does not subscribe to TopicA does not receive the message. These combinations do receive it:
# TopicA and TopicB
# TopicA and TopicC
# Conditions for topics support two operators per expression, and parentheses are supported.
# For more information, check: https://firebase.google.com/docs/cloud-messaging/topic-messaging
其他参数选项
collapse_key (str, optional): Identifier for a group of messages that can be collapsed so that only the last message gets sent when delivery can be resumed. Defaults to `None`. delay_while_idle (bool, optional): If `True` indicates that the message should not be sent until the device becomes active. time_to_live (int, optional): How long (in seconds) the message should be kept in FCM storage if the device is offline. The maximum time to live supported is 4 weeks. Defaults to ``None`` which uses the FCM default of 4 weeks. low_priority (boolean, optional): Whether to send notification with the low priority flag. Defaults to `False`. restricted_package_name (str, optional): Package name of the application where the registration IDs must match in order to receive the message. Defaults to `None`. dry_run (bool, optional): If `True` no message will be sent but request will be tested.
访问响应数据。
# Response from FCM Server.
response['multicast_id'] #Unique ID (number) identifying the multicast message.
response['success'] #Number of messages that were processed without an error.
response['failure'] #Number of messages that could not be processed.
response['canonical_ids'] #Number of results that contain a canonical registration token.
response['results'] #Array of objects representing the status of the messages processed.
result = [{response dict},...]
# The response objects are listed in the same order as the request (i.e., for each registration ID in the request,
# its response is listed in the same index in the response).
# message_id: String specifying a unique ID for each successfully processed message.
# registration_id: Optional string specifying the canonical registration token for the client app that the message
# was processed and sent to. Sender should use this value as the registration token for future requests. Otherwise,
# the messages might be rejected.
# error: String specifying the error that occurred when processing the message for the recipient
许可证
麻省理工学院许可证(MIT)。
Copyright (c) 2016 Lintel Technologies Pvt Ltd ( http://lintel.in ) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
致谢
Emmanuel Adegbite,PyFCM的作者
OperaSoftware的twisted-gcmclient。
大部分代码来自PyFCM,几行来自Opera软件的twisted-gcmclient代码。
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源代码分发
txFCM-0.0.1.zip (19.8 kB 查看哈希值)
txFCM-0.0.1.tar.gz (13.3 kB 查看哈希值)
关闭
txFCM-0.0.1.zip的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | da5a580452a440c91d21c440981e1ea904307aa703491c6d4016d34369b27aea |
|
MD5 | 08230b2782aedfd211f38d20006c3370 |
|
BLAKE2b-256 | 3f297cf5f3c2bfaf16c17abf196f539c3d00d7bfe4f7282d8ea3ae08177b9d4a |
关闭
txFCM-0.0.1.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 3e8354c3947eaa3aa8feca14fdcaf745633533c34bc8d9c223f11674e500dd69 |
|
MD5 | cd8a9bbd692fcd4c12ba636f219d67fa |
|
BLAKE2b-256 | 60f3269f8771ad868c2980ca73f789f64b1917a965b9cb619d7a612674c116c9 |