【发布时间】:2021-12-31 07:42:21
【问题描述】:
#我能够收到我的通知,但我想使用 sqflite 保存所有通知,并使用 listview 或卡片在我的应用程序中显示所有通知.....我是这个通知的新手,我不知道如何保存通知并将其显示在我的应用程序 uisng listview 或卡片中卡片视图或列表视图中我的应用程序内的通知列表
#this is in my main.dart
Future<void> backgroundNotificationHandler(RemoteMessage message) async{
print(message.data.toString());
print(message.notification!.title);
}
void main() async{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(backgroundNotificationHandler); //mathi ko future lai call garaya ko
runApp(const MyApp());
}
LocalNotificationService.initialize(context);
#this FirebaseMessaging.instance.getInitialMessage() comes on work when ever to show notification and when user taps it opens the app from terminated state
#simply saying app terminated state ma vhako bela notification send + app lai open garnekam garxa
FirebaseMessaging.instance.getInitialMessage().then((message){
if(message !=null){
final routeFromMessage = message.data["routeKey"];
Navigator.of(context).pushNamed(routeFromMessage);
}
});
#this onMessage will only get called when app is in foreground meaning running but not in background
FirebaseMessaging.onMessage.listen((message) {
if(message.notification!=null){
print(message.notification!.body);
print(message.notification!.title);
}
LocalNotificationService.display(message);
});
#this will comes in play when app is in background running and user taps on that notification and user is redirected to particular route
FirebaseMessaging.onMessageOpenedApp.listen((message) {
final routeFromMessage = message.data["routeKey"];
Navigator.of(context).pushNamed(routeFromMessage);
});
}
#This is my local notification
class LocalNotificationService{
static final FlutterLocalNotificationsPlugin _notificationsPlugin = FlutterLocalNotificationsPlugin();
static void initialize(BuildContext context){
final InitializationSettings initializationSettings = InitializationSettings(android: AndroidInitializationSettings("@mipmap/ic_launcher")); //we can pass icon
_notificationsPlugin.initialize(initializationSettings,onSelectNotification: (String? routeKey) async{ //yo onselectnotification le chai app chalda faree popup notification aauxa ne tesma route dine kam garinxa aaba
if(routeKey!=null){
Navigator.of(context).pushNamed(routeKey);
}
});
}
static display(RemoteMessage message) async{
try {
final id = DateTime.now().millisecond~/1000;
final NotificationDetails notificationDetails = NotificationDetails(android: AndroidNotificationDetails("com.example.pushnotification","com.example.pushnotification channel",channelDescription: "This is our channel",importance: Importance.max,priority: Priority.high));
//here payload show be described because above inside onSelectNotification we have given route and that data should be initialized in payload other wise we will get null route when tapping the popup notification
await _notificationsPlugin.show(id, message.notification!.title, message.notification!.body, notificationDetails,payload:message.data["routeKey"] );
} on Exception catch (e) {
print(e);
throw e;
}
}
}
【问题讨论】:
-
你不应该有一个API来从服务器获取所有通知吗?
-
我发送的上述代码是一个示例,说明我如何从我手动发送通知的 firebase 云消息传递中获取推送通知,但现在当我发送该通知并获取它时,我想保存该通知标题,使用 sqflite 发送消息并使用卡片设计或列表视图在我的应用程序中显示通知
标签: flutter firebase-cloud-messaging