【问题标题】:Sending push notification messages from my NodeJS app is not working从我的 NodeJS 应用程序发送推送通知消息不起作用
【发布时间】:2017-09-14 17:58:18
【问题描述】:

我正在尝试从我的NodeJS 服务器向我的iOS 设备发送推送通知,但我无法让它工作。我正在使用Send to topic 方法。我已经尝试了Firebase Admin (NodeJS) 文档中的代码示例,但它似乎不起作用。

这是我的 NodeJS 代码:

const admin = require('firebase-admin');    // Firebase-admin module to send push notifications via firebase
const serviceAccount = require('./myprivatekey.json'); //privateKey.json file

// setup firebase admin
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: FIREBASE_URL
});

sendMessage('someTopic', 'test message');

function sendMessage(topicName, message) {
    // The topic name can be optionally prefixed with "/topics/".
    var topic = `/topics/${topicName}`;
    console.log(topic);

    // See the "Defining the message payload" section below for details
    var payload = {
        notification: {
            title: 'test title',
            body: message
        }
    };

    // Send a message to devices subscribed to the provided topic.
    admin.messaging().sendToTopic(topic, payload)
        .then(function (response) {
            // See the MessagingTopicResponse reference documentation for the
            // contents of response.
            console.log('Successfully sent message:', response);
        })
        .catch(function (error) {
            console.log('Error sending message:', error);
        });
}

这是我订阅该主题的 Swift 代码:

override func viewDidLoad() {
        super.viewDidLoad()

        FIRMessaging.messaging().subscribe(toTopic: "/topics/someTopic")
    }

当我运行我的 NodeJS 应用程序时,我收到了 Successfully sent message: 日志,所以我不确定为什么我没有在我的 iOS 设备上收到推送通知。

是的,我已经为开发和生产设置了必要的APN 证书。我能够从Firebase Cloud Messaging Web 控制台发送推送通知,并在我的iOS 设备上成功接收通知。我似乎无法让我的 NodeJS Firebase Admin 应用程序工作。感谢您对此的任何帮助。

更新:我还想指出,即使使用 Swift 代码订阅 firebase 主题,指定的主题也不会出现在 Firebase Web 控制台上。我知道在 Firebase Web 控制台上出现需要几个小时,但自从我运行代码将我的 iOS 设备订阅到 firebase 主题已经有一周了。

我还尝试将我的 NodeJS 代码更改为发送到单个设备而不是发送到主题,这很有效。只是发送到主题不起作用。我已将可能的问题缩小到send to topic。难道是我拥有的 Swift 代码没有成功地将我的 iOS 设备订阅到指定的主题?但这正是我从 Firebase 文档中得到的确切 sn-p。

【问题讨论】:

  • 您是否实现了为远程通知和接收主题消息注册设备所需的所有代码? firebase.google.com/docs/notifications/ios/console-topics
  • @PatNeedham 是的。我可以验证这一点,因为我可以从 Firebase Web 控制台发送推送通知,并在我的 iOS 设备上成功接收通知。我似乎无法接收来自我的 Node 应用程序的通知

标签: ios node.js firebase firebase-cloud-messaging


【解决方案1】:

想通了。事实证明,在 viewDidLoad() 中调用订阅代码是一个坏主意,因为该应用可能在那个时候还没有完全连接到 FCM 和 APN。所以我在 AppDelegate 中的 didRegisterForRemoteNotificationsWithDeviceToken 中重新定位了该代码。

现在它正在工作。

我从这个 stackoverflow 问题线程中得到了这个想法:Cannot receive push notification on iOS from Firebase 3.2.0 topics

希望对遇到此问题的人有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 2012-10-24
    • 2019-09-11
    • 2013-02-28
    • 2012-12-20
    • 1970-01-01
    • 2018-07-06
    相关资源
    最近更新 更多