I am making an android app where I am using firebase authentication for authenticating users and firestore for storing some user data. I want to allow users to have a setting where they can subscribe to push notifications. If they subscribe, I want my app to send them notifications at regular intervals, say once every 6 hours. For now, I have tested the push notifications using the firebase console and it seems to work fine. Here is my MyFireBaseMessagingService.java
file so far:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken(@NonNull String token) {
sendRegistrationToServer(token);
}
public void sendRegistrationToServer(String token) {
UserApi.getInstance().setToken(token);// send the token to firestore
}
}
Now, I want to send a notification using FireBase Cloud Messaging. I am not able to understand how to achieve this. I tried searching a lot but the only way I can find is through using node.js
and cloud functions. But how do I achieve this ? My user document has a attribute which is set to true
for users who have subscribed to notifications. What would be the typescript code to automate the notification process for them? I am using Firebase for the first time.
EDIT 1: Also, when now I think about it, I think that I don't need the device token for achieving this maybe
EDIT 2: After looking at the comments by Frank, I am using this curl command to test if the push notifications are working for subscribed users
curl -X POST -H "Authorization: Bearer APP_SERVER_KEY" -H "Content-Type: application/json" -d '{
"message": {
"topic" : "notifications",
"notification": {
"body": "This is a Firebase Cloud Messaging Topic Message!",
"title": "FCM Message"
}
}
}' https://fcm.googleapis.com/v1/projects/PROJECT_ID/messages:send HTTP/1.1
For some reason, the above command doesn't work in powershell and gives the following erros:
curl: (6) Could not resolve host: is
curl: (6) Could not resolve host: a
curl: (6) Could not resolve host: Firebase
curl: (6) Could not resolve host: Cloud
curl: (6) Could not resolve host: Messaging
curl: (6) Could not resolve host: Topic
curl: (3) Illegal characters found in URL
curl: (3) [globbing] unmatched close brace/bracket in column 13
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}
curl: (6) Could not resolve host: HTTP
I am losing my hair thinking about what is wrong with this and why is it so hard ? I have gone over many SO links but none seem to solve my problem. After this works, I still need to schedule notifications which might be even harder? :/