BEEPIXL
 · CEO at BEEPIXL

awesome notification working with buttons

Troubleshooting

Hi I have implemented awesome notification and it's working fine when the app is in the foreground I am getting buttons and all. but when the app is in background I am getting 2 notifications one from firebase default and one from my awesome notification

I need to disable the firebase notification and handle it from awesomeNotifications with buttons

What have you tried so far?

// Automatic FlutterFlow imports

import '/backend/backend.dart';

import '/backend/schema/structs/index.dart';

import '/backend/sqlite/sqlite_manager.dart';

import '/flutter_flow/flutter_flow_theme.dart';

import '/flutter_flow/flutter_flow_util.dart';

import 'index.dart'; // Imports other custom actions

import '/flutter_flow/custom_functions.dart'; // Imports custom functions

import 'package:flutter/material.dart';

import '/backend/api_requests/api_calls.dart';

import '/flutter_flow/flutter_flow_util.dart';

// Begin custom action code

// DO NOT REMOVE OR MODIFY THE CODE ABOVE!


import 'package:awesome_notifications/awesome_notifications.dart';

import 'package:firebase_messaging/firebase_messaging.dart';


Future<void> notificationService() async {


await AwesomeNotifications().requestPermissionToSendNotifications();

// Initialize Awesome Notifications

AwesomeNotifications().initialize(

'resource://mipmap/ic_launcher',

[

NotificationChannel(

channelKey: 'basic_channel',

channelName: 'Basic notifications',

channelDescription: 'Notification channel for basic tests',

playSound: true,

enableVibration: true,

criticalAlerts: true,

)

],

debug: true,

);


await AwesomeNotifications().setListeners(

onActionReceivedMethod: onActionReceivedMethod,

onNotificationCreatedMethod: onNotificationCreatedMethod,

onDismissActionReceivedMethod: onDismissActionReceivedMethod,

onNotificationDisplayedMethod: onNotificationDisplayedMethod,

);


// Initialize Firebase Messaging

FirebaseMessaging messaging = FirebaseMessaging.instance;

messaging.subscribeToTopic('saleshandy');


// messaging.instance.requestPermission();


// Handle foreground messages

FirebaseMessaging.onMessage.listen((RemoteMessage message) async {

_handleMessage(message);

});


// Handle background messages

FirebaseMessaging.onBackgroundMessage(_firebaseMessageHandler);

}


Future<void> _firebaseMessageHandler(RemoteMessage message) async {

debugPrint("Message from Firebase: ${message.toMap().toString()}");

AwesomeNotifications().createNotificationFromJsonData(message.data);

Map<String, String?> payload = {

'subject': message.data['subject'],

'actionMarkAsRead': message.data['actionMarkAsRead'],

'actionView': message.data['actionView'],

'actionReply': message.data['actionReply'],

'hashId': message.data['hashId'],

'timestamp': message.data['timestamp']

};


await AwesomeNotifications().createNotification(

content: NotificationContent(

icon: 'resource://mipmap/ic_launcher',

id: message.notification.hashCode,

title: message.notification?.title ?? 'New Notification',

body: message.notification?.body ??

'You have received a new notification',

channelKey: 'basic_channel',

payload: payload),

actionButtons: [

NotificationActionButton(

key: 'REPLY',

label: 'Reply Message',

actionType: ActionType.Default),

NotificationActionButton(

key: 'DISMISS',

label: 'Dismiss',

actionType: ActionType.DismissAction,

isDangerousOption: true)

]);

}


void _handleMessage(RemoteMessage message) async {

debugPrint('Received message: ${message.toMap().toString()}');

Map<String, String?> payload = {

'subject': message.data['subject'],

'actionMarkAsRead': message.data['actionMarkAsRead'],

'actionView': message.data['actionView'],

'actionReply': message.data['actionReply'],

'hashId': message.data['hashId'],

'timestamp': message.data['timestamp']

};


await AwesomeNotifications().createNotification(

content: NotificationContent(

icon: 'resource://mipmap/ic_launcher',

id: message.notification.hashCode,

title: message.notification?.title ?? 'New Notification 2',

body: message.notification?.body ??

'You have received a new notification 2',

channelKey: 'basic_channel',

payload: payload),

actionButtons: [

NotificationActionButton(

key: 'REPLY',

label: 'Reply Message 2',

actionType: ActionType.Default),

NotificationActionButton(

key: 'DISMISS',

label: 'Dismiss',

actionType: ActionType.DismissAction,

isDangerousOption: true)

]);

}


// Static methods for notification listeners

Future<void> onNotificationCreatedMethod(

ReceivedNotification receivedNotification) async {

debugPrint('Notification Created!');

}


Future<void> onNotificationDisplayedMethod(

ReceivedNotification receivedNotification) async {

debugPrint('Notification Displayed!');

}


Future<void> onDismissActionReceivedMethod(

ReceivedAction receivedAction) async {

debugPrint('Notification Dismissed!');

}


Future<void> onActionReceivedMethod(ReceivedAction receivedAction) async {

final payload = receivedAction.payload ?? {};

debugPrint("Notification Action Tapped! Payload: ${payload.toString()}");


if (receivedAction.actionType == ActionType.SilentAction) {

debugPrint("Silent Action Code");

}


if (receivedAction.actionType == ActionType.Default) {}

}

Did you check FlutterFlow's Documentation for this topic?
Yes
2