【问题标题】:How can i save my push notification using sqflite and show the saved notification inside app using listview如何使用 sqflite 保存我的推送通知并使用 listview 在应用程序中显示保存的通知
【发布时间】: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


【解决方案1】:

我不确定您为什么要使用 LocalNotifications,因为FirebaseMessaging.onMessageOpenedApp.listen 总是会在您的应用处于后台时向您发送通知,

FirebaseMessaging.onMessageOpenedApp.listen((message) async {
  RemoteNotification notification = message.notification;

使用上面的代码,以便在 notification 中您将获得来自 firebase 的所有数据信息(标题、正文等),现在将此数据保存在您的 Sqlite 数据库或您想要的任何内容中处理您的数据。

【讨论】:

  • 谢谢你的回复,我会照你说的做的
  • 你能看看我尝试使用共享首选项在本地保存数据的代码
猜你喜欢
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多