I have done the setup according to the documentation but i am getting this error:
E/Android: [31m[Awesome Notifications](19182): Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
I have pasted my code in the next section.
How to show Local notifications in flutterflow using Awesome Notifications package
Integrations
//I am running this function in main.dart to initialize.
import 'package:awesome_notifications/awesome_notifications.dart';
Future initiateNotifications() async {
WidgetsFlutterBinding.ensureInitialized();
final context = WidgetsFlutterBinding.ensureInitialized().root;
print("----------- INIT noti ____------------");
AwesomeNotifications().initialize(
null, // Use default app icon
[
NotificationChannel(
channelKey: 'media_channel',
channelName: 'Media Playback',
channelDescription: 'Media control notifications',
importance: NotificationImportance.High,
playSound: false,
),
],
debug: true,
);
// Optionally, request permissions if not already granted
AwesomeNotifications().requestPermissionToSendNotifications();
// Set up the listener for action button taps
AwesomeNotifications().setListeners(
onActionReceivedMethod: onActionReceivedMethod,
);
print("----------- INIT noti finish ____------------");
}
Future<void> onActionReceivedMethod(ReceivedAction receivedAction) async {
if (receivedAction.buttonKeyPressed == 'TOGGLE_AUDIO') {
toggleAllPlayers(false, false);
}
}
Code to the function where i trigger a new notification:
import 'package:awesome_notifications/awesome_notifications.dart';
Future showAudioPlayerNotification() async {
print("--------------- SHOW NOTI-------------------");
final List<int> displayedNotifications =
await AwesomeNotifications().getAllActiveNotificationIdsOnStatusBar();
// Check if a notification with id=1 is already displayed
//bool alreadyDisplayed = false;
bool alreadyDisplayed =
displayedNotifications.isNotEmpty && displayedNotifications.contains(1);
if (!alreadyDisplayed) {
print("--------------- creating NOTI-------------------");
AwesomeNotifications().createNotification(
content: NotificationContent(
id: 1, // Unique notification id
channelKey: 'media_channel',
title: 'Dream Blend',
body: 'Falling into deep sleep',
notificationLayout:
NotificationLayout.MediaPlayer, // Media style layout
),
actionButtons: [
NotificationActionButton(
key: 'TOGGLE_AUDIO',
label: 'Play/Pause',
// You can configure autoDismiss or other properties if needed
)
],
);
}
}
Yes
2
1 reply