0

I've created an Android app with Firebase messaging. Notifications are working fine from the Firebase console. I want to make it work when I click a button from HTML page. Is this possible?

Bryan
  • 14,756
  • 10
  • 70
  • 125
dilanMD
  • 95
  • 2
  • 12

2 Answers2

1

To send a message to devices via the Firebase Cloud Messaging API, you need to pass in the so-called FCM Server Key.

As its name implies, this key should only be used in trusted environment, such as a server you control, your development machine, or Cloud Functions. The reason for that is that somebody who has the FCM Server Key can send any message they want to any of your users.

So if you embed this key in an untrusted environment (such as your web page), a malicious user can simply copy it, and send messages on your behalf. And your users will have no way to know what message came from you, and what messages came from the malicious users.


The typical solution is to implement the sending of messages in a trusted environment, such as in Cloud Functions. That way your (server-side) code can ensure that the sender is authorized to send messages.

For more on this, see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I was using the KEY and TOKEN for some credential issues I put the key_string as KEY and token_string as TOKEN – dilanMD Nov 19 '19 at 03:58
0

Finally it is working from the below coding

$.ajax({        
        type : 'POST',
        url : "https://fcm.googleapis.com/fcm/send",
        headers : {
            Authorization : my_key
        },
        contentType : 'application/json',
        dataType: 'json',
        data: JSON.stringify({
            "to": my_token, 
            "notification": {
                "title":"Test",
                "body":"Test"
                }
            }),
        success : function(response) {
            console.log(response);
        },
        error : function(xhr, status, error) {
            console.log(xhr.error);                   
        }
    });
dilanMD
  • 95
  • 2
  • 12