簡體   English   中英

Firebase 雲 function 向 iPhone 發送通知

[英]Firebase cloud function send notification to iPhone

我正在嘗試通過以下代碼觸發對 iOS 的推送通知。 我已經實現了這一點,當我通過 Firebase 控制台觸發它們時它工作正常。 但我無法通過雲功能觸發它們。

當我運行這個 function 我得到{"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}

我在這里錯過了什么嗎?

exports.testMessage = functions.https.onCall(() => { 

  console.log('test message started');

  const registrationToken = 'fPxlvSiOYEX6gyGhSb5zZp:APA91bH3Q10Yjmo9Oa0Glx78_EZJQ3LO0B1PPWhfOZkTthOI7aP9uUsvzAq3EmYkfaswWIjsTR57NEKkr8BF2q4UbWH4-C5AqGv_XGvJp5C2EPbQ3cjL4zo0eGcSr3IP0PmSpYLjh1Sm';
  
  var message = {
      notification: {
          title: 'Test title',
          body:'Test body'
      },
      token: registrationToken // Token is inserted here. 
  };
  
  return admin.messaging().send(message)
      .then((response) => {
          // Response is a message ID string.
          console.log('Successfully sent message:', response);
          return { 'message': 'sent' } 
      }).catch((error) => {
          console.log('Error sending message:', error);
          return { 'message': 'failed' }     
      });
  })

根據官方文檔,您必須傳遞設備的注冊令牌,如圖所示

向特定設備發送消息

// This registration token comes from the client FCM SDKs.
var registrationToken = 'YOUR_REGISTRATION_TOKEN';

var message = {
  data: {
    score: '850',
    time: '2:45'
  },
  token: registrationToken
};

// Send a message to the device corresponding to the provided
// registration token.
admin.messaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });


暫無
暫無

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

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