簡體   English   中英

如何將 firbase 函數從 (functions.https.onRequest) 更改為 (functions.https.onCall)?

[英]How to change the firbase function from(functions.https.onRequest) to (functions.https.onCall)?

我已經完成了實現通知的firebase功能。

所以我嘗試更改函數 Type ,但失敗了..如何更改此代碼?

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));
  
});

從這段代碼到..

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





 });

這個。

如果我正確理解了您的問題,您首先需要了解onRequest() (類似 HTTPS-Express)和onCall() (可調用)Firebase 函數之間的主要區別。

文檔

請務必記住,HTTPS 可調用函數與 HTTP 函數相似但不完全相同。 Callables 與 HTTP 函數有以下主要區別:

  • 對於可調用對象,Firebase 身份驗證和 FCM 令牌(如果可用)會自動包含在請求中。
  • functions.https.onCall 觸發器會自動反序列化請求正文並驗證身份驗證令牌。

有關更詳盡的解釋,請參閱可調用的雲函數是否比 HTTP 函數更好?

這是一些未經測試的代碼,您可以使用它們開始:

    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);
  );

請注意, data可能包含消息文本,而context參數表示用戶身份驗證信息。

context包含有關請求的元數據,例如 uid 和令牌,可以在此處找到更多信息。

編輯

可以在此處找到定義payload示例代碼。 可以在此處找到sendToDevice()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM