0

i've already completed the firebase function that realized notification.

so i tried to change function Type , but fail.. how to change this code?

exports.sendNotification = functions.https.onRequest((request,response)=>{

  const id = request.body.user_id;
  const pw = request.body.user_pw;
  const yourToken = request.body.yourToken;
  const myToken = request.body.myToken;

  var payload = {
    notification:{
      title : "notify",
      body : "suceess to send."
    },
    data:{
      title : "notify",
      body : "suceess to send.",
      user_id : id,
      user_pw : pw,
      myToken : myToken,
      yourToken : yourToken,
    }
  };
  
  response.send(admin.messaging().sendToDevice(yourToken, payload));
  
});

from this code to ..

exports.Notification = functions.https.onCall((data,context)=>{





 });

this.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
김영환
  • 1
  • 1

1 Answers1

0

If I understood your question correctly, you will need to first understand the main differences between onRequest() (HTTPS-Express like) and onCall() (Callable) Firebase Functions.

From documentation:

It's important to keep in mind that HTTPS callable functions are similar but not identical to HTTP functions. Callables have these key difference from HTTP functions:

  • With callables, Firebase Authentication and FCM tokens, when available, are automatically included in requests.
  • The functions.https.onCall trigger automatically deserializes the request body and validates auth tokens.

For a more exhaustive explanation, see Are Callable Cloud Functions better than HTTP functions?

This is some code, which is untested, that you can use to get started:

    exports.sendToDevice = functions.https.onCall(async (data, context) => {
    
      // Data passed from the client using the Callable
      const id = data.user_id;
      const pw = data.user_pw;
      const yourToken = data.token;
      const myToken = data.myToken;

      var payload = {
        notification:{
           title : "notify",
           body : "suceess to send."
        },
        data:{
           title : "notify",
           body : "suceess to send.",
           user_id : id,
           user_pw : pw,
           myToken : myToken,
           yourToken : yourToken,
        }
      };

      return await admin.messaging().sendToDevice(yourToken, payload);
  );

Please note that data could contain the message text, while context parameters represent user auth information.

context contains metadata about the request such as uid and token, more information can be found here.

Edited:

Sample code to define payload can be found here. The sendToDevice() method can be found here.

sllopis
  • 2,292
  • 1
  • 8
  • 13
  • Oh I really appreciate your help. There was little reference. thanks a lot!! – 김영환 Oct 23 '20 at 00:46
  • But there is some problem in (admin.messaging.MessagingPayload) . Because Type annotations can only be used in TypeScript files. What can I do instead this code? – 김영환 Oct 23 '20 at 01:05
  • I just edited my answer and added more references to the documentation for the `sendToDevice()` method. I hope it helps. – sllopis Oct 23 '20 at 08:25