cim*_*mus 6 rest android push-notification firebase
是否可以在Firebase上使用REST API发送推送通知?我可以使用Firebase控制台发送通知,但是我需要使用REST API发送通知。
Tee*_*rat 11
这可能会有所帮助 - https://firebase.google.com/docs/cloud-messaging/http-server-ref 您可以在这里找到示例消息 - https://firebase.google.com/docs/cloud-messaging/downstream
从 Firebase 控制台,您可以获得服务器密钥作为您在 http 标头中放置的授权,在标签云消息传递中。
das*_*sum 11
I used the below rest API to send notification.
curl -X POST \
https://fcm.googleapis.com/fcm/send \
-H 'Authorization: key=AAAAG-oB4hk:APA91bFUilE6XqGzlqtr-M-LRl1JisWgEaSDfMZfHuJq3fs7IuvwhjoGM50i0YgU_qayJA8FKk15Uvkuo7SQtQlVt4qdcrrhvnfZyk_8zRGAskzalFUjr2nA2P_2QYNTfK6X8GbY0rni' \
-H 'Content-Type: application/json' \
-H 'Postman-Token: c8af5355-dbf2-4762-9b37-a6b89484cf07' \
-H 'cache-control: no-cache' \
-d '{
"to": "ey_Bl_xs-8o:APA91bERoA5mXVfkzvV6I1I8r1rDXzPjq610twte8SUpsKyCuiz3itcIBgJ7MyGRkjmymhfsceYDV9Ck-__ObFbf0Guy-P_Pa5110vS0Z6cXBH2ThnnPVCg-img4lAEDfRE5I9gd849d",
"data":{
"body":"Test Notification !!!",
"title":"Test Title !!!"
}
}'
Run Code Online (Sandbox Code Playgroud)
Authorization : key=AAAAG-oB4hk:APA91bFUilE6XqGzlqtr-M-LRl1JisWgEaSDfMZfHuJq3fs7IuvwhjoGM50i0YgU_qayJA8FKk15Uvkuo7SQtQlVt4qdcrrhvnfZyk_8zRGAskzalFUjr2nA2P_2QYNTfK6X8GbY0rni
where key is web_server_key from the console and you need to specify the unique registration key which you will get from the app.
"to": "ey_Bl_xs-8o:APA91bERoA5mXVfkzvV6I1I8r1rDXzPjq610twte8SUpsKyCuiz3itcIBgJ7MyGRkjmymhfsceYDV9Ck-__ObFbf0Guy-P_Pa5110vS0Z6cXBH2ThnnPVCg-img4lAEDfRE5I9gd849d" is the FCM registration token from device. Please refer to the below link.
https://firebase.google.com/docs/cloud-messaging/android/client?authuser=0
Ash*_*aha 11
只是为了帮助
如果有人想使用REST POST API,请使用以下配置的邮递员
网址:
https://fcm.googleapis.com/fcm/send
标头:
"Content-Type": "application/json",
"Authorization": "key=<Server_key>"
Run Code Online (Sandbox Code Playgroud)
身体:
{
"to": "<Device FCM token>",
"notification": {
"title": "Check this Mobile (title)",
"body": "Rich Notification testing (body)",
"mutable_content": true,
"sound": "Tri-tone"
},
"data": {
"url": "<url of media image>",
"dl": "<deeplink action on tap of notification>"
}
}
Run Code Online (Sandbox Code Playgroud)
而已。谢谢!!!
如果您想获得有关带有FCM的Rich Notification的更多详细信息,可以查看我的有关使用Firebase Cloud Messaging(FCM)和iOS平台上的Pusher的中级丰富推送通知的文章。
小智 9
尝试这个,
网址 - https://fcm.googleapis.com/fcm/send
方法 -后
标头
身体
{
"to" : "FCM Token goes here",
"notification" : {
"body" : "New Lesson Added 1",
"title": "Lokesh"
}
}
Run Code Online (Sandbox Code Playgroud)
新版本的 API(称为 v1)为通过 ARC 发送消息带来了更多挑战。您需要一个特殊的令牌,该令牌将会过期。您必须在 firebase 控制台中创建 firebase admin sdk 密钥(服务帐户密钥):
它们的密钥以 json 格式存储,如下所示:
{
"type": "service_account",
"project_id": "<your project ID>",
"private_key_id": "8b..............................37",
"private_key": "-----BEGIN PRIVATE KEY-----
MIIE.....
....
-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-6fzie@<yourprojectID>.iam.gserviceaccount.com",
"client_id": "1...................4",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url":
"https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-
6fzie%40<yourprojectID>.iam.gserviceaccount.com"
}
Run Code Online (Sandbox Code Playgroud)
该密钥用于在获取 http 通信令牌时识别您的身份。您需要一种对 firebase 的服务器访问权限。我在 WSL 中使用了 python 和这段代码:
import requests
import google.auth.transport.requests
from google.oauth2 import service_account
SCOPES = ['https://www.googleapis.com/auth/firebase.messaging']
credentials = service_account.Credentials.from_service_account_file('service-account.json', scopes=SCOPES)
request = google.auth.transport.requests.Request()
credentials.refresh(request)
print(credentials.token)
Run Code Online (Sandbox Code Playgroud)
其中 service-account.json 是运行 python 的文件系统上文件中的私钥。您将获得令牌,并且可以在 ARC 内使用它:
ya29.c.b0Aa9VdylWfrAFWysdUeh3m7cGF-Cow1OAyyE4bEqFL....................48Ye7w
Run Code Online (Sandbox Code Playgroud)
ARC 配置与旧版 API 类似,但有一些变化。URL 已更改,其中包含您的项目 ID:
https://fcm.googleapis.com/v1/projects/<your project ID>/messages:send
Run Code Online (Sandbox Code Playgroud)
我们仍然使用 POST 方法,并且标头只有一行 Content-Type application/json。身份验证有一个单独的选项卡,我们应该使用 python 中的 Bearer + token:
选择承载并启用它非常重要,因为默认情况下它是禁用的。
身体也发生了变化。这是基于应用程序令牌向单个应用程序发送消息的示例:
{
"message" : {
"token" : "e6e....FurF",
"notification" : {
"body" : "Great deal!",
"title" : " Buy everything"
}
}
}
Run Code Online (Sandbox Code Playgroud)
其中关键字“to”已更改为“token”。这就是全部,我们可以将消息发送到应用程序。我希望将它放在这里,以便能够按照 Goggle 目前的要求迁移到 API v1。最后一段代码是用于curl的:-)
curl " https://fcm.googleapis.com/v1/projects/<your project id>/messages:send" \
-X POST \
-d "{\r\n \"message\" : {\r\n \"token\" : \"e6e....FurF\",\r\n \"notification\" : {\r\n \"body\" : \"Great deal!\",\r\n \"title\" : \" Buy everything\"\r\n }\r\n }\r\n}" \
-H "Content-Type: application/json" \
-H "authorization: Bearer ya29.c.b...."
Run Code Online (Sandbox Code Playgroud)
这是我使用过的来源:
您可以使用ARC或Postman或您自己的服务器发送通知。您需要从控制台收集web_server_key,并且需要指定在调用该onRefreshToken()
方法时将从应用程序获取的唯一注册密钥。
您需要使用Content-Type:json和Authorization:web_server_key将请求发送到https://fcm.googleapis.com/fcm/send。启用要为用户赋值app_registration_token。
归档时间: |
|
查看次数: |
25637 次 |
最近记录: |