首页 > 解决方案 > 当 Flutter 应用程序终止时,普通推送通知会静默出现或根本不出现

问题描述

我在 nodejs 上使用 firebase-admin 向用户发送推送通知: https ://firebase.google.com/docs/admin/setup

我在颤振上使用 firebase_messaging: https ://pub.dev/packages/firebase_messaging

我正在尝试在 android 和 ios 上发送具有最高优先级的推送通知。

firebase.messaging(firebaseApp).send({
    token,
    notification,
    android: { priority: 'high' },
    apns: { headers: { 'apns-priority': '10' }, payload: { aps: { sound: 'default' } } },
    data
}

在我的开发设备上,当应用程序在后台和终止时,它都像一个魅力。

我在 android 和 ios 上都收到了 100% 明显弹出并发出声音的推送通知。

问题出在其他设备上 -

如果应用程序在后台,我有时会看到一个实际的推送通知明显弹出,并发出应有的声音。有时不是。

如果应用程序终止,则会收到推送通知 - 但它不会明显弹出也不会发出声音,如果我从屏幕顶部向下滚动菜单,我只能看到它,它被列为收到的推送通知。有时根本没有收到通知。

我也尝试在实际设备上将通知的优先级从我的应用程序更改为高,但它没有改变任何东西。

我的用户一直抱怨他们没有收到推送通知——我猜他们要么实际上没有收到它们,要么根本看不到它们,因为正如我上面描述的那样,它们是默默地收到的。

我真的不知道发生了什么以及为什么它在我自己的开发设备以外的设备上不能正常工作。

有任何想法吗?

标签: node.jsfirebaseflutterpush-notificationfirebase-cloud-messaging

解决方案


当我们从 firebase api 发送推送时,有两个标签需要完整

  1. 优先
  2. android_channel_id

如果 android 7.0 或 8.0 以上的设备没有收到 android_channel_id 则不会显示通知,因此始终在 android manifest 中设置默认通知通道。我们可以检查我们通过节点设置的 json 请求中的可用标签。 从这里

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id"/>

像这样并确保您在移动端创建通知通道。

String CHANNEL_ID = "your_channel id";
String CHANNEL_DESCRIPTION = "Your description to show in settings";


NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_DESCRIPTION, NotificationManager.IMPORTANCE_HIGH);
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);

如果您看到 Flutter 应用程序日志,它会显示一些错误,例如清单中未设置默认通道或通知中未找到通道。

确保将日志记录在您的问题中以解决此问题或与我联系以解决问题。

还要确保您 从此处的 fcm flutter lib阅读Optionally handle background messages部分, 他们提到在 android 文件夹中添加 fcm。


推荐阅读