【问题标题】:Showing an iOS app badge through FirebaseMessaging background messages通过 FirebaseMessaging 背景消息显示 iOS 应用徽章
【发布时间】:2021-11-17 19:05:36
【问题描述】:

我正在用 Flutter 开发一个应用程序,但遇到了一些问题。我想要发生的是

  1. 通过某些事件,使用 FCM 向用户发送推送通知。我同时发送notificationdata 消息,以便调用onBackgroundMessage 处理程序。
  2. 每次收到消息时,处理程序都会将徽章计数增加 1。

我的消息负载如下所示:

const payload = {
    notification: {  
        title: String(data["title"]),
        body: String(data["message"]),
    },
    data: {
        title: data["title"],
        body: data["message"],
    },
}
const payloadBackground = {
    data: {
        title: data["title"],
        body: data["message"],
    },
    content_available: true,
    priority: "high"
}

这是使用 FirebaseMessaging 作为 Firebase 云函数运行的:

admin.messaging().sendToDevice(receiverRegistrationTokens, payloadBackground).catch(err => console.error(err));
return admin.messaging().sendToDevice(receiverRegistrationTokens, payload).then( response => {
    console.log(receiver + " has registrationTokens: " + receiverRegistrationTokens);
    return null;
})

在客户端,通知是这样处理的:

Future<void> backgroundNotificationHandler(RemoteMessage message) {
  FlutterAppBadger.updateBadgeCount(1);
  debugPrint("Received notification: " + message.data.toString());
  return null;
}
AwesomeNotifications().initialize(
    // set the icon to null if you want to use the default app icon
    'resource://drawable/res_app_icon',
    [
      NotificationChannel(
        channelKey: 'basic_channel',
        channelName: 'Basic notifications',
        channelDescription: 'Notification channel for basic tests',
        defaultColor: Color(0xFF9D50DD),
        ledColor: Colors.white,
        enableLights: true,
      )
    ]);
FirebaseMessaging.onBackgroundMessage(backgroundNotificationHandler);

由于某种原因,iOS 上的徽章数量没有增加(根本没有徽章)。但是,推送通知会正确显示。关于如何解决这个问题的任何想法?非常感谢!

【问题讨论】:

  • 你在 backgroundNotificationHandler 中得到debugPrint 输出了吗?
  • 不,我不知道,但我想这是正常的,因为只有在应用程序处于后台或终止时才会调用处理程序
  • onBackgroundMessage 是在任何类之外的顶级函数吗?你在iOS模拟器真机上试试吗?
  • 是的,这是一个顶级功能,我们已经通过 TestFlight 在真实的 iOS 设备上对其进行了测试,以确保推送通知包含所有必要的证书
  • 在 iOS 上,后台通知只会在后台到达,而不是在终止时到达。我不确定,但尝试将content_available 放在notification 中,之前确实需要一条纯数据消息,但也许它适用于较新版本中的通知。

标签: ios flutter firebase-cloud-messaging


【解决方案1】:

我在我的环境中尝试过,以下 Node.js Firebase 函数可调用确实会在真实 iOS 设备上触发后台消息,如果我在后台消息处理程序中添加 print,我可以在调试控制台中看到输出:

// send a test message
exports.testMessage = functions.https.onCall(async (data, context) => {
    try {
        await admin.messaging().sendToDevice(
            '<TOKENS>', {
            data: {
                key1: 'value1',
            },
            notification: {
                title: 'Title1',
                body: 'Body1'
            }
        }, {
            contentAvailable: true,
            priority: "high",
        });
    } catch (e) {
        console.log(e);
    }
});

【讨论】:

  • 嗯,好的,谢谢你的帮助,我稍后会试试这个,如果它有效,请告诉你! :)
  • @JesseGeens 我收到了您的编辑建议。请查看页面底部的here。它说priority 可以是high,并且必须在几秒钟内使用timeToLive 设置消息过期时间。
  • here,它说priority作为文本“正常”或“高”将分别转换为APNs优先级5和10。
猜你喜欢
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 2018-09-08
  • 2013-01-10
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
相关资源
最近更新 更多