【问题标题】:Firebase cloud function send notification to iPhoneFirebase 云功能向 iPhone 发送通知
【发布时间】:2021-04-04 00:04:45
【问题描述】:

我正在尝试通过以下代码向 iOS 触发推送通知。我已经实现了这个,当我通过 Firebase 控制台触发它们时它工作正常。但我无法通过云功能触发它们。

当我运行这个函数时,我得到{"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' }     
      });
  })

【问题讨论】:

    标签: ios node.js google-cloud-functions firebase-cloud-messaging apple-push-notifications


    【解决方案1】:

    根据官方文档,您必须如图所示传递设备的注册令牌

    Send messages to specific devices

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

    【讨论】:

    • 我需要将它包装在一个函数中吗?我仍然收到 {"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}
    • 是的,你必须将它包装在一个函数中。请考虑“使用 functions.https.onCall 创建 HTTPS 可调用函数。This method 采用两个参数:数据和可选上下文:”
    猜你喜欢
    • 2019-02-23
    • 2018-08-05
    • 2018-06-01
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多