[英]Flutter push notifications app background : firebase_messaging
I want to display a push notification when my app is in the background.我想在我的应用程序处于后台时显示推送通知。
I am using the flutter_local_notifications package and the firebase_messaging package.我正在使用flutter_local_notifications package 和firebase_messaging package。
Push notifications are working great with firebase_messaging and when my app is the background.推送通知与 firebase_messaging 以及我的应用程序作为后台时配合得很好。
However, the below method:但是,下面的方法:
// The following handler is called, when App is in the background.
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
is not called if a RemoteNotification
object is passed through the RemoteMessage
:如果RemoteNotification
object 通过RemoteMessage
传递,则不会调用:
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print('message from background handler');
print("Handling a background message: ${message.messageId}");
// If this is null, then this handler method is called
RemoteNotification remoteNotification = message.notification;
showNotificationFromData(message.data);
}
Therefore, i have to pass the message.data
object which is a Map<String, dynamic>
.因此,我必须传递message.data
object 这是一个Map<String, dynamic>
。
My problem is that i don't receive push notification anymore when this firebaseMessagingBackgroundHandler
handler method is called.我的问题是,当调用此firebaseMessagingBackgroundHandler
处理程序方法时,我不再收到推送通知。
So i have been trying to use the flutter_local_notification
package to display a push notification but as it is said, it's "local" so it's working fine in the Foreground but apparently not in the background (the same code, with the same data is not showing in the background as a push notification).所以我一直在尝试使用flutter_local_notification
package 来显示推送通知,但正如所说,它是“本地”的,所以它在前台工作正常但显然不在后台(相同的代码,相同的数据未显示在后台作为推送通知)。
Question:问题:
I need to call the firebaseMessagingBackgroundHandler
handler to handle events in my app.我需要调用firebaseMessagingBackgroundHandler
处理程序来处理我的应用程序中的事件。 So i can do to still have push notifications when my app is in the background please?所以当我的应用程序在后台时,我可以做些什么来仍然有推送通知吗?
Thanks谢谢
Okay i have found the solution.好的,我找到了解决方案。 It was to set the content_available
value to True
in the server-side code.它是在服务器端代码中将content_available
值设置为True
。
To replace the firebase_messaging.Notification
item, we need to override to the android
and apns
configs.要替换firebase_messaging.Notification
项,我们需要覆盖android
和apns
配置。
This looks like this in python:这在 python 中看起来像这样:
apnsConfig = messaging.APNSConfig(
payload=messaging.APNSPayload(
aps=messaging.Aps(
content_available=True,
)
),
)
I am now receiving push notifications and call the background handler inside the dart code.我现在正在接收推送通知并调用 dart 代码中的后台处理程序。
I know this bit too late but anyone trying to get custom notification using firebase then here is the solution我知道这有点太晚了,但任何人都试图使用 firebase 获得自定义通知,那么这里是解决方案
For Android (Flutter)对于 Android(颤振)
On Android, notification messages are sent to Notification Channels which are used to control how a notification is delivered.在 Android 上,通知消息被发送到用于控制通知传递方式的通知通道。 The default FCM channel used is hidden from users, however provides a "default" importance level.使用的默认 FCM 通道对用户是隐藏的,但提供了“默认”重要性级别。 Heads up notifications require a "max" importance level.注意通知需要“最大”重要性级别。
This means that we need to first create a new channel with a maximum importance level & then assign incoming FCM notifications to this channel.这意味着我们需要首先创建一个具有最高重要性级别的新通道,然后将传入的 FCM 通知分配给该通道。 we can take advantage of the flutter_local_notifications
package我们可以利用flutter_local_notifications
package
flutter_local_notifications
package to your local project.将flutter_local_notifications
package 添加到本地项目。const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications.', // description
importance: Importance.max,
);
(keep in mind this is example code but you can custimize notification through notification channel code) (请记住,这是示例代码,但您可以通过通知渠道代码自定义通知)
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
android/app/src/main/AndroidManifest.xml
file for your FlutterProject project.为此,打开 FlutterProject 项目的android/app/src/main/AndroidManifest.xml
文件。 Add the following meta-data
schema within the application
component:在application
组件中添加以下meta-data
模式: <meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />
now whenever you send notification specify your channel id and your app automatically show your custom notification (through api or firebase console)现在,每当您发送通知时指定您的频道 ID,您的应用程序会自动显示您的自定义通知(通过 api 或 firebase 控制台)
{
"message":{
"token":"token_1",
"android_channel_id":"high_importance_channel" //specify channel id here
"data":{},
"notification":{
"title":"FCM Message",
"body":"This is an FCM notification message!",
}
}
}
as for ios they don't support notification channel i don't have necessary resources and info how things works if you guys know comment below will be much appriciated至于ios ,他们不支持通知渠道,我没有必要的资源和信息,如果你们知道下面的评论将会非常有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.