如何在应用程序打开(前景)时显示推送通知,当 OnMessage 被触发时?
How to show a push-notification when the application is open(foreground), when OnMessage is triggered?
我使用 Flutter 和 Firebase 消息传递。
我在示例中配置 Firebase:
firebaseMessaging.configure(
onMessage: ...
onLaunch: ...
onResume: ...
)
但我想即使在应用程序打开时也能看到推送通知。
粗略地说 onMessage 应该像 onResume 一样工作。
我该怎么做?
onMessage: (Map<String, dynamic> message) async {
showNotification(message);
print('on message $message');
}
showNotification(Map<String, dynamic> msg) async {
var android = new AndroidNotificationDetails(
'your channel id',//channel id
"your channel name",//channel name
"your channel description",//channel desc todo set all this right
icon: 'mipmap/launcher_icon'//add your icon here
);
var iOS = new IOSNotificationDetails();
var platform = new NotificationDetails(android, iOS);
await flutterLocalNotificationsPlugin
.show(0, msg['notification']['title'], msg['notification']['body'], platform);
}
我使用 flutter_local_notifications: ^1.2.2 显示本地通知前景。
此外,如果您要为 IOS 实施,请不要忘记请求通知许可。
我使用 Flutter 和 Firebase 消息传递。
我在示例中配置 Firebase:firebaseMessaging.configure(
onMessage: ...
onLaunch: ...
onResume: ...
)
但我想即使在应用程序打开时也能看到推送通知。
粗略地说 onMessage 应该像 onResume 一样工作。
我该怎么做?
onMessage: (Map<String, dynamic> message) async {
showNotification(message);
print('on message $message');
}
showNotification(Map<String, dynamic> msg) async {
var android = new AndroidNotificationDetails(
'your channel id',//channel id
"your channel name",//channel name
"your channel description",//channel desc todo set all this right
icon: 'mipmap/launcher_icon'//add your icon here
);
var iOS = new IOSNotificationDetails();
var platform = new NotificationDetails(android, iOS);
await flutterLocalNotificationsPlugin
.show(0, msg['notification']['title'], msg['notification']['body'], platform);
}
我使用 flutter_local_notifications: ^1.2.2 显示本地通知前景。
此外,如果您要为 IOS 实施,请不要忘记请求通知许可。