Reputation: 593
I have a backend server build on Delphi and I use Google FCM push notification service through pure HTTP requests. After Google implemented FCM API HTTP V1, my backend could not send push notifications anymore.
According my review of documentations and examples, to send a push notification to an device now it is required two steps :
Get a Bearer Token
Use this Bearer token in the HTTP POST request authorization part of the request to the new endpoint of messages : https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send
Since I just use pure HTTP requests and I do not plan to build a code in another language that uses FCM Admin SDK.
Is there any endpoint in the HTTP API in which I can get the Bearer Token? Or is there any way to send PUSH notification using API HTTP V1 without this Bearer Token?
A last question : is it possible to use FCM Admin SDK on Delphi?
Upvotes: 1
Views: 674
Reputation: 11
You need to generate Access token with service_account.json that you can download from Firebase Console.
for my case, im using Python3 for backend, there are 3 ways of sending Notification, im using firebase_admin:
data = {
"title": "Notification title",
"body": "Description"
}
is_android = True
device_tokens = ["token1", "token2"]
if is_android:
message = messaging.MulticastMessage(
tokens=device_tokens,
data=data,
android=messaging.AndroidConfig(
data=data
),
)
else:
message = messaging.MulticastMessage(
notification=messaging.Notification(title=data.get("title"), body=data.get("body")),
tokens=device_tokens,
data={k: v for k, v in data.items() if k not in ("body", "title", "badge")},
apns=messaging.APNSConfig(
payload=messaging.APNSPayload(
aps=messaging.Aps(
content_available=True,
badge=1,
mutable_content=True,
custom_data=data,
alert=messaging.ApsAlert(
title=data.get("title"),
body=data.get("body")
)
),
custom_data=data
),
),
)
if data:
message.data = data
response = messaging.send_each_for_multicast(message)
from pyfcm import FCMNotification
fcm = FCMNotification(service_account_file="/location/to/project-firebase-admin-sdk.json", project_id="project-name")
result = fcm.notify(
fcm_token='device-token',
notification_title="Test",
notification_body="Test desc",
data_payload={
"payload_key1": "value123"
},
)
from google.oauth2 import service_account
import google.auth.transport.requests
def get_access_token():
credentials = service_account.Credentials.from_service_account_file(
"/location/to/project-firebase-admin-sdk.json", scopes=['https://www.googleapis.com/auth/firebase.messaging']
)
request = google.auth.transport.requests.Request()
credentials.refresh(request)
return credentials.token
data = {
"message": {
"token": "device_token",
"data": {
"param1": "value",
},
"notification": {
"title": "Notification title",
"body": "Description",
},
"apns": {
"headers": {
"apns-priority": "10",
},
"payload": {
"aps": {
"mutable-content": 1,
"content_available": 1,
"contentAvailable": True,
},
"type": "feed",
"data": {
"title": "Notification title",
"body": "Description",
"type": "feed"
},
"content_available": True,
"priority": 'high',
"apns-priority": 10,
}
}
}
}
response = post("https://fcm.googleapis.com/v1/projects/>PROJECT_ID</messages:send", json=data, headers= {
"Content-Type": "application/json",
"Authorization": f"Bearer {get_access_token()}"
})
I think last piece for generating access token will help you. Keep in mind that they changed request data that is sent Here
Upvotes: 1