Azure 通知中心提供易于使用和横向扩展的推送引擎。使用通知中心向任何平台(iOS、Android、Windows)以及任何后端(云或本地)发送通知。 有关详细信息,请参阅什么是 Azure 通知中心?。
在本快速入门中,你将使用通知中心中的平台通知系统(PNS)设置在多个平台上设置推送通知。 本快速入门演示了使用 Azure 门户时要执行的步骤。
Google Firebase Cloud Messaging 包含 Azure CLI 使用说明。
如果尚未创建通知中心,请立即创建一个通知中心。 有关详细信息,请参阅在 Azure 门户中创建 Azure 通知中心或使用 Azure CLI 创建 Azure 通知中心。
Apple Push Notification 服务
要设置 Apple Push Notification 服务 (APNS):
在 Azure 门户的“通知中心”页上,从左侧菜单中选择“Apple (APNS)”。
对于“身份验证模式”,请选择“证书”或“令牌” 。
如果选择“证书”:
- 选择文件图标,再选择要上传的 .p12 文件。
- 输入密码。
- 选择沙盒模式。 要将推送通知发送给从应用商店购买应用的用户,则选择生产模式。
如果选择“令牌”:
- 输入密钥 ID、绑定 ID、团队 ID 和令牌的值。
- 选择沙盒模式。 要将推送通知发送给从应用商店购买应用的用户,则选择生产模式。
有关详细信息,请参阅使用 Azure 通知中心向 iOS 应用发送推送通知。
Google Firebase Cloud Messaging V1 (FCMv1)
为 Google FCMv1 设置推送通知:
- 在 Azure 门户的“通知中心”页上,从左侧菜单中选择“Google (FCMv1)”。
- 从 Firebase 控制台获取的服务帐户 JSON 文件中更新“私钥”、“项目 ID”和“客户端电子邮件”的值。
- 选择“保存”。
完成这些步骤后,会显示一条警报,指出已成功上传通知中心。 “保存”按钮已禁用。
Google Firebase Cloud Messaging (FCM)
注意
Firebase Cloud Messaging (FCM) 已被弃用,不再受支持。
若要为 Google FCM 设置推送通知,请执行以下操作:
在 Azure 门户的“通知中心”页上,从左侧菜单中选择“Google (GCM/FCM)”。
为之前保存的 Google FCM 项目粘贴“API 密钥”。
选择“保存”。
完成这些步骤后,会显示一条警报,指出已成功上传通知中心。 “保存”按钮已禁用。
需要 Google Firebase Cloud Messaging (FCM) 项目的 API 密钥 。
为 Azure CLI 准备环境
- 本文需要 Azure CLI 版本 2.0.67 或更高版本。 如果使用 Azure Cloud Shell,则已安装最新版本。
为 Google FCM 设置推送通知
使用 az notification-hub credential gcm update 命令将 Google API 密钥添加到通知中心。
az notification-hub credential gcm update --resource-group spnhubrg --namespace-name spnhubns --notification-hub-name spfcmtutorial1nhub --google-api-key myKey
Android 应用需要连接字符串才能连接到通知中心。 使用 az notification-hub authorization-rule list 命令列出可用的访问策略。 使用 az notification-hub authorization-rule list-keys 命令获取访问策略连接字符串。 在 参数中指定 primaryConnectionString 或 --query
,以直接获取主连接字符串。
#list access policies for a notification hub
az notification-hub authorization-rule list --resource-group spnhubrg --namespace-name spnhubns --notification-hub-name spfcmtutorial1nhub --output table
#list keys and connection strings for a notification hub access policy
az notification-hub authorization-rule list-keys --resource-group spnhubrg --namespace-name spnhubns --notification-hub-name spfcmtutorial1nhub --name myAccessPolicyName --output json
#get the primaryConnectionString for an access policy
az notification-hub authorization-rule list-keys --resource-group spnhubrg --namespace-name spnhubns --notification-hub-name spfcmtutorial1nhub --name myAccessPolicyName --query primaryConnectionString
使用 az notification-hub test-send 命令测试向 Android 应用发送消息的操作。
#test with message body
az notification-hub test-send --resource-group spnhubrg --namespace-name spnhubns --notification-hub-name spfcmtutorial1nhub --notification-format gcm --message "my message body"
#test with JSON string
az notification-hub test-send --resource-group spnhubrg --namespace-name spnhubns --notification-hub-name spfcmtutorial1nhub --notification-format gcm --payload "{\"data\":{\"message\":\"my JSON string\"}}"
可以使用 az notification-hub credential 命令获取其他平台的 Azure CLI 参考。
若要详细了解如何将通知发送到 Android 应用程序,请参阅使用 Firebase 将推送通知发送到 Android 设备。
Windows 推送通知服务
要设置 Windows 推送通知服务 (WNS):
在 Azure 门户的“通知中心”页上,从左侧菜单中选择“Windows (WNS)”。
输入“包 SID”和“安全密钥”的值。
选择“保存”。
相关信息,请参阅通过 Azure 通知中心向 UWP 发送通知。
完成这些步骤后,警报指示通知中心已成功更新。 “保存”按钮已禁用。
有关详细信息,请参阅借助百度的通知中心入门。
后续步骤
在该快速入门中,你学习了如何在 Azure 门户中为通知中心配置平台通知系统设置。
要详细了解如何将通知推送到各类平台,请参阅以下教程: