使用 FlutterFire 的 Firebase 自定义通知

Firebase custom notifications with FlutterFire

感谢 Firebase 和 FlutterFire,可以轻松地从服务器向用户设备发送定期通知。这些通知包括标题、body 和图像 url。但是创建一个 no-that-simple 通知怎么样,比如 Telegram 或 WhatsApp 的通知?

简单的问题是避免从服务器发送Notification,而是将data字段设置为推送消息。但是根据 the FlutterFire documentation:

Data only messages are considered low priority by devices when your application is in the background or terminated, and will be ignored

所以,听起来如果我们想要一个可靠的传递系统,我们应该在我们的推送消息中添加一个 Notification。但是那个通知就是这么简单。再一次,根据 the documentation:

If your message is a notification one (includes a notification property), the Firebase SDKs will intercept this and display a visible notification to your users (assuming you have requested permission & the user has notifications enabled)

所以:如果我想要一个可靠的系统,我必须发送 Notifications,但是我发送了,我不能告诉 FlutterFire 使用我的自定义通知。

所以问题是:如何使用 FlutterFire 显示自定义通知?

我想达到的效果是这样的:

我要尝试设置推送通知的优先级。

You can however explicitly increase the priority by sending additional properties on the FCM payload:

On Android, set the priority field to high.
On Apple (iOS & macOS), set the content-available field to true.

在服务器端代码,它看起来像:

message.setAndroidConfig(AndroidConfig.builder().setPriority(AndroidConfig.Priority.HIGH).build());
message.setApnsConfig(ApnsConfig.builder().setAps(Aps.builder().setContentAvailable(true).build()).build());