Flutter awesome notification click open specific page

220

To do this, firstly you need to initialize AwesomeNotifications before runApp and then simply put a listner to listen to the notification click:

Initialize:

AwesomeNotifications().initialize(
        'resource://drawable/logo_circle_notification',
        [
          NotificationChannel(
              channelGroupKey: 'normal_channel_group',
              channelKey: 'normal_channel',
              channelName: 'Normal Notifications',
              channelDescription: 'Notification channel for normal notifications',
              defaultColor: const Color(0xFF9D50DD),
              ledColor: Colors.white
          ),
        ],
        channelGroups: [
          NotificationChannelGroup(
              channelGroupkey: 'basic_channel_group',
              channelGroupName: 'Basic group'),
        ],
        debug: true
    );

Listen:

listenActionStream(){
        AwesomeNotifications().actionStream.listen((receivedAction) {
          var payload = receivedAction.payload;
    
          if(receivedAction.channelKey == 'normal_channel'){
            //do something here
          }
        });
      }

you can put that lister in the initState of your splash screen or something before navigating to Home Screen of the app.

Share:
220
zey
Author by

zey

Updated on December 01, 2022

Comments

  • zey
    zey over 1 year

    I am using Flutter awesome notifications. When the notification is clicked when the application is closed, I want to direct it to a special page within the application. What is the easiest way for me to do this?

  • zey
    zey about 2 years
    really thank you :) I have been trying this for days.
  • Siddharth Mehra
    Siddharth Mehra about 2 years
    you're welcome! :)
  • easyscript
    easyscript almost 2 years
    pls how do I use default call ringtone as notification sound?
  • Siddharth Mehra
    Siddharth Mehra almost 2 years
    @easyscript use defaultRingtoneType: DefaultRingtoneType.Ringtone in NotificationChannel when you initialize Awesome Notifications.
  • easyscript
    easyscript almost 2 years
    @SiddharthMehra Thank you, I really appreciate, Now I have two challenges, (1) How do I pass and receive additional data? it seems to have title and body only but i have user data I want to use. (2) I receive background notification using AwesomeNotifications().createdStream.listen and in it I navigate to specific page, is working BUT my app remains in the background, pls how do I bring the app to foreground without user clicking notification?
  • Siddharth Mehra
    Siddharth Mehra almost 2 years
    @easyscript (1) you can pass the payload when creating a notification and also receive it in the listener when clicking on the notification. (2) please explain more about what you want to archive exactly. I think I've misunderstood what you asked.
  • easyscript
    easyscript almost 2 years
    (2) When app is in background and notification is received using AwesomeNotifications().createdStream.listen, I want to bring up the app to foreground. I dont know if my explaination is clear
  • Siddharth Mehra
    Siddharth Mehra almost 2 years
    Okay, understood, I am not an expert on this but I don't really think it is possible without any user interaction.