消防云传输(Android,IOS and Web)

pyfcm的Python项目详细描述


versionlicense

用于fcm的python客户端-firebase云消息(android、ios和web)

firebase云消息(fcm)是gcm的新版本。它继承了可靠和可扩展的gcm基础设施,以及新功能。强烈建议GCM用户升级到FCM。

使用fcm,您可以通知客户端应用程序可以同步新电子邮件或其他数据。您可以发送通知来驱动用户重新登录和保留。对于即时消息之类的用例,消息可以将高达4KB的负载传输到客户端应用程序。

有关详细信息,请访问:https://firebase.google.com/docs/cloud-messaging/

快速启动

使用pip安装:

pip install pyfcm

OR

pip install git+https://github.com/olucurious/PyFCM.git

pyfcm支持android、ios和web。

功能

  • 涵盖所有FCM功能
  • 龙卷风支援

示例

使用FCMNotification类发送通知:

# Send to single device.frompyfcmimportFCMNotificationpush_service=FCMNotification(api_key="<api-key>")# OR initialize with proxiesproxy_dict={"http":"http://127.0.0.1","https":"http://127.0.0.1",}push_service=FCMNotification(api_key="<api-key>",proxy_dict=proxy_dict)# Your api-key can be gotten from:  https://console.firebase.google.com/project/<project-name>/settings/cloudmessagingregistration_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"result=push_service.notify_multiple_devices(registration_ids=registration_ids,message_title=message_title,message_body=message_body)printresult

发送数据消息。

# 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.# 3. Notification messages with optional data payload.# 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 payloaddata_message={"Nick":"Mario","body":"great match!","Room":"PortugalVSDenmark"}# To multiple devicesresult=push_service.notify_multiple_devices(registration_ids=registration_ids,message_body=message_body,data_message=data_message)# To a single deviceresult=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 also do NOT include notification body# To multiple devicesresult=push_service.multiple_devices_data_message(registration_ids=registration_ids,data_message=data_message)# To a single deviceresult=push_service.single_device_data_message(registration_id=registration_id,data_message=data_message)# To send extra kwargs (notification keyword arguments not provided in any of the methods),# pass it as a key value in a dictionary to the method being usedextra_notification_kwargs={'android_channel_id':2}result=push_service.notify_single_device(registration_id=registration_id,data_message=data_message,extra_notification_kwargs=extra_notification_kwargs)# To process background notifications in iOS 10, set content_availableresult=push_service.notify_single_device(registration_id=registration_id,data_message=data_message,content_available=True)# To support rich notifications on iOS 10, setextra_kwargs={'mutable_content':True}# and then write a NotificationService Extension in your app# 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.# PyFCM 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 == Falseresult=push_service.notify_multiple_devices(registration_ids=registration_ids,message_body=message,low_priority=True)

获取有效的注册ID(用于清除数据库中的无效注册ID)

registration_ids=['reg id 1','reg id 2','reg id 3','reg id 4',...]valid_registration_ids=push_service.clean_registration_ids(registration_ids)# Shoutout to @baali for this

appengine用户应定义其环境

push_service=FCMNotification(api_key="<api-key>",proxy_dict=proxy_dict,env='app_engine')result=push_service.notify_multiple_devices(registration_ids=registration_ids,message_body=message,low_priority=True)

管理对主题的订阅

push_service=FCMNotification(SERVER_KEY)tokens=[<registration_id_1>,<registration_id_2>,]subscribed=push_service.subscribe_registration_ids_to_topic(tokens,'test')# returns True if successful, raises error if unsuccessfulunsubscribed=push_service.unsubscribe_registration_ids_from_topic(tokens,'test')# returns True if successful, raises error if unsuccessful

向主题发送消息。

# Send a message to devices subscribed to a topic.result=push_service.notify_topic_subscribers(topic_name="news",message_body=message)# Conditional topic messagingtopic_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 PyFCM.response_dict={'multicast_ids':list(),# List of Unique ID (number) identifying the multicast message.'success':0,#Number of messages that were processed without an error.'failure':0,#Number of messages that could not be processed.'canonical_ids':0,#Number of results that contain a canonical registration token.'results':list(),#Array of dict objects representing the status of the messages processed.'topic_message_id':Noneorstr}# 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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java检查整数是0还是检查变量是null更好?   java Android Kotlin(初学者)使用File(),并从ACTION\u GET\u内容返回Uri   java JavaFx在“内部场景”和根场景之间切换   spring将XMLBean配置转换为java配置   java JPA HIBERNATE映射列两次(embeddedID和POJO)   c#单态模式模型在什么情况下适用?   java请求。getRemoteUser在特定时间后返回null?   spring boot中PUT api控制器的java my单元测试用例失败   java在字符串中互换地解析和替换值   java Android JNI在应用程序中检测到错误:调用JNI GetMethodID时出现挂起异常   JavaSpringDataMongo:使用非简单键持久化映射   爪哇玻璃鱼连接被拒绝   java如何在用户注册时发送特定电子邮件id的自动回复?   Java列表:实例化时和之后的赋值之间的差异