[英]Flutter - Firebase push notification using firebase messaging success but not get notification
[英]Flutter Firebase messaging - push notification is not showing when app is open
我是 flutter 的新手,我只是想接收 firebase 推送通知到我的 flutter 應用程序。 當應用程序關閉並在后台接收推送通知。 但是當應用程序打開時,推送通知正在接收,但它沒有顯示警報通知(如果它已打開,我想在我的應用程序中將推送通知標題和正文顯示為警報)。 這是我的代碼。
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: ListTile(
title: Text(message['notification']['title']),
subtitle: Text(message['notification']['body']),
),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
onPressed: () => Navigator.of(context).pop(),
),
],
),
);
print("onMessage: $message");
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
有人可以幫我嗎?
最后,我能夠通過使用overlay_support包來管理我的問題
我參考了以下問題鏈接:
Flutter - Firebase Messaging Snackbar 未顯示
我按照以下教程和包管理了我的問題
教程: https : //medium.com/flutter-community/in-app-notifications-in-flutter-9c1e92ea10b3
包: https : //pub.dev/packages/overlay_support/install
我將MaterialApp()
小部件包裝在OverlaySupport()
小部件中。
return OverlaySupport(
child: MaterialApp(....
));
然后我將showOverlayNotification
添加到我的 _fcm.configure --> onMessage:
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
showOverlayNotification((context) {
return Card(
margin: const EdgeInsets.symmetric(horizontal: 4),
child: SafeArea(
child: ListTile(
leading: SizedBox.fromSize(
size: const Size(40, 40),
child: ClipOval(
child: Container(
color: Colors.black,
))),
title: Text(message['notification']['title']),
subtitle: Text(message['notification']['body']),
trailing: IconButton(
icon: Icon(Icons.close),
onPressed: () {
OverlaySupportEntry.of(context).dismiss();
}),
),
),
);
}, duration: Duration(milliseconds: 4000));
print(message['notification']['title']);
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
當應用程序在前台時用戶收到通知時,您可以使用Get包來顯示小吃店。
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
Get.snackbar("message['notification']['title']", snackPosition: SnackPosition.TOP,);
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
'snackPosition' 參數允許小吃店顯示在頂部,因此顯示為警告消息。
還有就是如何結合FCM使用flutter_local_notifications包一個很好的解釋在這里
FCM 為您提供了三個回調。 OnResume
, OnLaunch
和OnMessage
。
當應用程序在前台時, onMessage
被觸發,它讓你有機會執行任何自定義操作。
為了在應用程序處於前台時顯示通知,您可以使用Flutter Local Notifications包。
由於 onMessage 回調中缺少上下文,您可能無法看到警報對話框。 嘗試將_fcm.configure
包裝在里面
SchedulerBinding.instance.addPostFrameCallback((_){ [_fcm.configure block] });
伙計們,如果您已將手機連接到您的電腦並正在測試它,它不會顯示通知。同樣的事情發生在我身上,所以我構建了 apk 然后再次嘗試它確實有效
"實際上 Android 的默認行為就像應用打開時不顯示通知一樣。
因此,如果您想在 App 打開時顯示通知,請add below line after initializing FirebaseApp and FirebaseMessaging
。
FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(alert: true,badge: true,sound: true);
應用打開時處理通知的新方法如下:
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
await _handleMessage(message);
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
await _handleMessage(message);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.