The request was missing an Authentication Key with push notification in firebase - firebase

I'm trying to send a push notification via REST API Firebase by Postman for specific user I followed below request pattern:
POST request with below URL :
https://fcm.googleapis.com/fcm/send?key=**my Web API key**
and the Body is
{ "data":
{
"title": "Firebase notification",
"detail": "I am firebase notification. you can customise me. enjoy"
},
"to" : "USER UID"
}
but the problem is it returns The request was missing an Authentication Key.

Kindly follow the steps as per bellow :
API URL: https://fcm.googleapis.com/fcm/send/
Request Method Post
Add 2 Key-value pairs in Header section like this
Content-Type : application/json
Authorization: key=[Your server key] (Make sure that no space allowed)
For body choose raw data (JSON)
{ "data": { "title": "Firebase notification", "detail": "I am firebase notification. you can customise me. enjoy" }, "to" : "USER UID" }

Related

FCM registration token - is it possible to find token value in app data?

I need to get a FCM registration token value which is on my device - just only to test push notification on my device before publishing it to all users. I know that there is a way to get a token during registration and send it to server, but maybe it's possible to find it in some app data without additional implementation?
Just print token to console and test it using postman.
var token = await FirebaseMessaging.getInstance().getToken();
print("token= "+token);
Then copy the token from the console and post using this api:
URI: https://fcm.googleapis.com/fcm/send
METHOD: POST
HEADERS: {
'Content-Type': 'application/json',
'Authorization': 'key=YOUR_FIREBASE_SERVER_KEY'
}
BODY: {
"notification":{
"body": "TEST MESSAGE",
"title": "TEST MESSAGE",
},
"priority": "high",
"data": {
"id": "1",
"status": "done"
},
"registration_ids": "THE_TOKEN",
},
Don't forget to close your app before sending the fcm request.

Flutter send firebase notification to token

So i have a chat app and i want it so that i can get a notification once someone sends me a message. The thing is that i implemented firebase messaging and i can receive a notification if i send it from firebase. I want to add it such as once i press the send button i can directly send the notification to a token within the app for instance something like:
send notification{
to token : here the receiver token
title : the title of the notification
description : the description of the notification
}
is it possible to have something like this?
Just for helping,
If anyone wants to use REST POST API, here it is, use the Postman with below configuration
URL:
https://fcm.googleapis.com/fcm/send
Header:
"Content-Type": "application/json",
"Authorization": "key=<Server_key>"
BODY:
{
"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>"
}
}

Firebase REST API signInWithCustomToken and Apple SignIn Provider

Context
iOS app written in Swift for iOS 13.2 (does NOT use Firebase iOS SDK)
App uses Firebase Realtime Database via REST calls (so needs an ID token from
Firebase Authentication)
I am trying to switch my iOS App to using Apple's "Sign in with Apple" feature, to replace the simple username / password authentication through the Firebase Authentication's "Email/Password" Sign-in provider .
A successful "Apple sign-in" by the user will leave me with a JWT token (identity token) from Apple, such as this one (decoded & edited) :
HEADER
{
"kid": "ABCDPK1",
"alg": "RS256"
}
BODY
{
"iss": "https://appleid.apple.com",
"aud": "XYZ.App",
"exp": 1574131940,
"iat": 1574131340,
"sub": "01234.1234561ecd054cd285d65cc7af261958.0123",
"c_hash": "ABCDe-csTpNdUa0Eri5wFw",
"email": "user#privaterelay.appleid.com",
"email_verified": "true",
"is_private_email": "true",
"auth_time": 1574131340
}
Problem
When I make use of the Firebase Authentication REST API to try and exchange a custom token for an ID and refresh token I get the following message (and yes, I have made sure the token wasn't expired when I used it) :
{
"error": {
"code": 400,
"message": "INVALID_CUSTOM_TOKEN",
"errors": [
{
"message": "INVALID_CUSTOM_TOKEN",
"domain": "global",
"reason": "invalid"
}
]
}
}
I was hoping that the new "Apple" sign-in method in Firebase Authentication would take care of that, once configured, and I could just pass in the JWT token from Apple. Am I missing a step ?
Thanks !
Ok, I guess I found the answer to my own question shortly after posting this (of course...).
I was using the wrong API endpoint. Instead I needed to use the Sign-in with OAuth credentials endpoint :
https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp?key={{apiKey}}
to get back :
{
"federatedId": "apple.com/ABCDEF.ABCDEF261958.1234",
"providerId": "apple.com",
"email": "user#privaterelay.appleid.com",
"emailVerified": true,
"localId": "123456786RbgQgxhzfSpyIJGLPIw1",
"idToken": "eyJABCDEFG.ABCDEFG.ABCDEFGiFQ",
"refreshToken": "AEu4IL1CY_PGF...6pN0DuQ",
"expiresIn": "3600",
"oauthIdToken": "eyJraWQiO...CJHHrxeg",
"rawUserInfo": "[JWT token info]",
"isNewUser": true,
"kind": "identitytoolkit#VerifyAssertionResponse",
"pendingToken": "AMzJoSn...HkvLeaOmFjNlcw"
}
The provided POST body should look something like :
{
"postBody": "id_token=eyJraWQiOiJBSURPUABCDEFGHIJKLMNOP.eyABCDEFGHIJKLMNOP2.KW0JABCDEFGHIJKLMNOP&providerId=apple.com",
"requestUri": "https://gpsstreaming.firebaseapp.com/__/auth/handler",
"returnIdpCredential": true,
"returnSecureToken": true
}
Note that the id_token is the 3-dot-notation token (JWT token) provided by Apple's Sign In With Apple ASAuthorizationAppleIDCredential -- data obfuscated here for obvious reasons. For example if you use SwiftUI, you get that from the completionBlock of the SignInWithAppleButton.

Need help registering Google Cloud Function URL as verified domain to receive push notifications

Am trying to call google calendar's push notification apis. I have written a google cloud function which just returns a response containing request body and headers. I want to use this google cloud function url as the receiving url for the push notification. The google docs mention steps to verify and register the domains, as part of which it asks me to upload a binary. Not sure how do upload a binary against this cloud function domain. My domain looks like https://asia-northeast1-project-name.cloudfunctions.net
This was my request for registering push notification:
Request:
POST
https://www.googleapis.com/calendar/v3/calendars/primary/events/watch?key={YOUR_API_KEY}
{ "id": "404713b9-d880-437d-b890-83715f01af7c", "type": "web_hook",
"address":
"https://asia-northeast1-huddle-quick-solutions.cloudfunctions.net/suratCalendarNotifications" }
Response:
{ "error": { "errors": [ {
"domain": "global",
"reason": "push.webhookUrlUnauthorized",
"message": "Unauthorized WebHook callback channel: https://asia-northeast1-project-name.cloudfunctions.net/path" } ], "code": 401, "message": "Unauthorized WebHook callback
channel:
https://asia-northeast1-project-name.net/path"
} }

Firebase Cloud Messaging sound error

I'm trying to send a notification using firebase api and the notification is sent successfully if I only have "title" and "body" in the notification JSON object. However, if I add "sound":"default" to the notification object, as described in the documentation, I get the following error:
"Invalid JSON payload received. Unknown name \"sound\" at 'message.notification': Cannot find field."
My JSON object is as follows:
{"message":{"token": token, "notification":{"title":"Test", "body":"Test message from server", "sound":"default"}}}
The appearance of message in your JSON indicates you are using the HTTP v1 API. The documentation you linked is for the legacy API.
The HTTP v1 API JSON to send a notification with sound for Android and iOS devices should be:
{
"message": {
"token": "your-token-value",
"notification": {
"title": "Test",
"body": "Test message from server"
},
"android": {
"notification": {
"sound": "default"
}
},
"apns": {
"payload": {
"aps": {
"sound": "default"
}
}
}
}
}

Resources