当前位置: 首页> 健康> 美食 > 建站宝盒免费下载_淮南电商网站建设费用_搜索引擎是什么意思_网站服务器一年的费用

建站宝盒免费下载_淮南电商网站建设费用_搜索引擎是什么意思_网站服务器一年的费用

时间:2025/8/2 10:45:21来源:https://blog.csdn.net/qq_38779672/article/details/146606275 浏览次数:1次
建站宝盒免费下载_淮南电商网站建设费用_搜索引擎是什么意思_网站服务器一年的费用

APNS推送

from apns2.client import APNsClient
from apns2.payload import Payload
from apns2.credentials import TokenCredentials# 配置
AUTH_KEY_PATH = 'xxxxx.p8'  # .p8 文件路径
TEAM_ID = 'xxxxxx'                     # Apple 开发者团队 ID
KEY_ID = 'xxxxx'                       # .p8 文件的密钥 ID
DEVICE_TOKEN = 'xxxxxx'                # 目标设备的令牌
TOPIC = 'xxxxxxxxx'                    # App 的 Bundle ID# 创建 Payload
payload = Payload(alert="Hello, APNs with .p8!",  # 通知内容sound="default",                # 提示音badge=1,                        # 角标custom={"key": "value"}         # 自定义数据
)# 使用 Token 认证
credentials = TokenCredentials(auth_key_path=AUTH_KEY_PATH,  # .p8 文件路径auth_key_id=KEY_ID,           # 密钥 IDteam_id=TEAM_ID               # 团队 ID
)# 创建 APNs 客户端
client = APNsClient(credentials=credentials,use_sandbox=True,  # 测试环境使用 sandbox,生产环境设置为 Falseuse_alternative_port=False
)# 发送通知
try:response = client.send_notification(DEVICE_TOKEN,payload,topic=TOPIC)print("Notification sent successfully!")print("APNs response:", response)
except Exception as e:print("Failed to send notification:", e)

FCM推送

json文件来自 管理台-项目设置-服务账号-Firebase Admin SDK-生成新的私钥

import firebase_admin
from firebase_admin import credentials
from firebase_admin import messaging# 初始化SDK(服务账号方式)
cred = credentials.Certificate('xxxxxx.json')
firebase_admin.initialize_app(cred)def send_fcm_notification(registration_token, title, body, data=None):"""发送单设备推送参数:registration_token : str - 设备注册令牌title : str - 通知标题body : str - 通知内容data : dict - 自定义键值对数据"""message = messaging.Message(notification=messaging.Notification(title=title,body=body),data=data or {},# 添加此处可以在点击时拉起app的处理,需要在AndroidManifest.xml对应处理# # 需要在主activity下添加如下# <intent-filter>#     <action android:name="NOTIFICATION_OPEN" />#     <category android:name="android.intent.category.DEFAULT" /># </intent-filter># # 在主activity中的onNewIntent中Intent可以获取到data内数据android=messaging.AndroidConfig(notification=messaging.AndroidNotification(click_action='NOTIFICATION_OPEN'  # Android端匹配的action)),token=registration_token)try:response = messaging.send(message)print('成功发送:', response)return Trueexcept Exception as e:print('发送失败:', str(e))return False# 使用示例
send_fcm_notification(registration_token='xxxxxxxxxx',title='温度警报',body='当前温度已超过阈值38℃',data={'sensor_id': 'temp-001', 'value': '38.5'}
)
关键字:建站宝盒免费下载_淮南电商网站建设费用_搜索引擎是什么意思_网站服务器一年的费用

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: