0

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? :/

nmnsharma007
  • 235
  • 3
  • 13

1 Answers1

1

To send messages through Firebase Cloud Messaging, you need to call its API and specify the FCM server key. As its name implies, this key should only be used on servers - or otherwise trusted environments, such as your development machine or Cloud Functions. For more on this, see How to send one to one message using Firebase Messaging.

Once you have code calling the FCM API, you'll need to target the right devices. There are two main options for that, either you know the device's token and you target the individual device, or you have the device subscribe to a so-called topic and then send the message to that topic too. For more on this, see: How do you send a Firebase Notification to all devices via CURL?

If you want to schedule the sending of the messages, you'll need a place where you can run code on a schedule. You can do this on your own development machine, or on a server you control, but it is also possible in Cloud Functions. For more on this, see How can scheduled Firebase Cloud Messaging notifications be made outside of the Firebase Console?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Before I ask anything else, I will have a good look at those links. Thanks :) – nmnsharma007 Sep 04 '21 at 14:24
  • Reading up those was quite overwhelming for me. I was thinking of creating a topic and letting users subscribe to them and then I need a firebase-cli for sending out messages and so I need to write some Javscript code. Then only can I start worrying about sending at regular intervals. Looks like too tough of a job for one man alone :( – nmnsharma007 Sep 04 '21 at 18:02
  • You don't need the Firebase CLI to send messages. You can for example do this from any command line/terminal with CURL, as shown int he second link in my answer. – Frank van Puffelen Sep 04 '21 at 18:54
  • Yea, I am trying to use curl in powershell and trying to run this command for users who have subscribed to the topic https://firebase.google.com/docs/cloud-messaging/android/topic-messaging#build_send_requests , and I think I need to schedule this request for regular intervals if I am not wrong. – nmnsharma007 Sep 05 '21 at 07:01
  • So, Yea I think I know what I should do now even though the curl script still doesn't work. Either way, the regular scheduling requires me use cloud functions and enable billing, I have decied to just abandon the idea. Your solution would have worked if I was more people. Thanks anyway :) – nmnsharma007 Sep 05 '21 at 10:28