Skip to content

Commit

Permalink
fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rahman committed May 1, 2021
1 parent 65ff4c2 commit e2a174a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 39 deletions.
109 changes: 73 additions & 36 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:admin/routes.dart';
import 'package:admin/themes/style.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Expand Down Expand Up @@ -167,6 +168,7 @@ class Application extends StatefulWidget {
}

class _Application extends State<Application> {
int lastMessageId = 0;
Future selectNotification(String payload) async {
print("payload $payload");
if (payload != null) {
Expand All @@ -183,60 +185,95 @@ class _Application extends State<Application> {
);
}

Future onDidReceiveLocalNotification(
int id, String title, String body, String payload) async {
// display a dialog with the notification details, tap ok to go to another page
showDialog(
context: context,
builder: (BuildContext context) => CupertinoAlertDialog(
title: Text(title),
content: Text(body),
actions: [
CupertinoDialogAction(
isDefaultAction: true,
child: Text('Ok'),
onPressed: () async {
Navigator.of(context, rootNavigator: true).pop();
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NotificationPage(),
),
);
},
)
],
),
);
}

@override
void initState() {
super.initState();
getPrefs();

print("firebase message is setuping...");

FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage message) {
log('message recived');
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
log("firebase message is onMessage ${message}");
NotificationProvider notificationProvider =
Provider.of<NotificationProvider>(context, listen: false);
DashboardProvider homeProvider =
Provider.of<DashboardProvider>(context, listen: false);
await homeProvider.fetchDashData();

await notificationProvider.fetchNotifications(pageParams: 1);

int messageId = int.parse(message.messageId);

print("load Home and notification");
if (lastMessageId != messageId) {
lastMessageId = messageId;

RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
NotificationProvider notificationProvider =
Provider.of<NotificationProvider>(context, listen: false);
DashboardProvider homeProvider =
Provider.of<DashboardProvider>(context, listen: false);
await notificationProvider.fetchNotifications(pageParams: 1);
await homeProvider.fetchDashData();

await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
print("load Home and notification");
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);

// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('app_icon');
final InitializationSettings initializationSettings =
InitializationSettings(android: initializationSettingsAndroid);

await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: selectNotification);

if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
icon: 'launch_background',
),
));
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('app_icon');
final IOSInitializationSettings initializationSettingsIOS =
IOSInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveLocalNotification);
final InitializationSettings initializationSettings =
InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS);

await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: selectNotification);

if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
icon: 'launch_background',
),
));
}
}
});
}
Expand Down
10 changes: 8 additions & 2 deletions lib/modules/notifications/notification_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ class NotificationPage extends StatelessWidget {
return value.fetchNotifications(pageParams: 1);
},
child: value.notificatins.isEmpty
? Center(child: Text("No Notification"))
? ListView(
children: [
Container(
height: MediaQuery.of(context).size.height-100,
child: Center(child: Text("No Notification"))),
],
)
: IncrementallyLoadingListView(
hasMore: () => value.hasMoreItems,
itemCount: () => value.notificatins.length,
Expand Down Expand Up @@ -148,7 +154,7 @@ class _NotificationItemState extends State<NotificationItem> {
children: [
Row(
children: [
Text("${widget.notification.body}"),
Expanded(child: Text("${widget.notification.body}")),
],
),
SizedBox(height: 10),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class NotificationProvider with ChangeNotifier {
loadedProducts.add(NotificationModel.fromJson(notify));
});

if (notificatins == null) {
if (notificatins == null || pageParams != null) {
notificatins = [];
}
notificatins.addAll(loadedProducts);
Expand Down

0 comments on commit e2a174a

Please sign in to comment.