繁体   English   中英

Firebase功能未发送通知

[英]Firebase Functions not sending Notification

我在其中一个新闻应用程序上使用功能约2周。 但是,我用不同的新闻创建了第二个新闻应用程序。 PN的调用使用Web挂钩进行,该挂钩将有关文章的数据发送给该主题的所有订阅者。 如果我将Web挂钩用于以前的应用程序,则它运行良好。 我已经使用了旧应用程序中的相同功能代码,并将其粘贴到新应用程序中,但是它无法正常工作。

我有:

  • 当我使用FCM / APNS发送通知时,检查我是否在iPhone上收到有关特定主题的通知-工作
  • 检查通知的代码和格式-可以
  • 测试是否调用了其他功能-工作
  • 电话已订阅推送-是
  • 设备已订阅主题-是。 经过FCM主题推送测试。
  • 已启用方法切换-是

这是功能的代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();


exports.sentNotify = functions.https.onRequest((request, response) => {
 console.log("notification request received");
 console.log(request.body);
 let topic = "Martin";//request.body.primaryCategory;
 let hottness = request.body.hottness;
 let title = request.body.title;
 let description = request.body.description;
 let thumbnail = request.body.thumbnail;
 let link = request.body.url;

 const messagePayload = {
   notification: {
     title: title,
     body: description,
     icon: 'thumbnail',
     sound: 'default',
     badge: '0'
   },
   data:{
     link_url: link,
     category: "NEWS_CATEGORY"
   }
 };

 const options = {
        priority: "high",
        timeToLive: 60 * 60 * 24
    };

 return admin.messaging().sendToTopic(topic, messagePayload, options)
.then(() => {
    response.send("OK")
    return;
    });
});

这是我发送的请求:

curl -X "POST" "https://us-central1-<my-app>.cloudfunctions.net/sentNotify" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{
  "hotness": 7000,
  "thumbail": "thumbnail",
  "title": "Title",
  "publishedAt": "2018-08-24T12:37:08.000Z",
  "description": "This is the description",
  "primaryCategory": "General",
  "url": "https://google.com"
}'

您是在发送消息之前向客户端发送响应:

response.send("OK")

当您从HTTPS类型的函数发送响应时,它将有效地终止该函数。 后续的任何代码都可能不会执行 (不确定),因为Cloud Functions保留在发送响应后限制所有其他资源的权利。 这意味着您的消息可能根本不会发送。

完成所有工作,包括异步工作(例如发送消息) ,才从HTTPS函数发送响应。 这意味着您应该安排仅在消息最终传递之后才发送响应:

return admin.messaging().sendToTopic(topic, messagePayload, options)
.then(() => {
    response.send("OK")
})

有关更多详细信息,请参见文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM