Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent ReceivedNotification from being displayed #91

Closed
MrWaggel opened this issue Jan 20, 2021 · 4 comments
Closed

Prevent ReceivedNotification from being displayed #91

MrWaggel opened this issue Jan 20, 2021 · 4 comments
Labels
help wanted Extra attention is needed

Comments

@MrWaggel
Copy link

Is there any way that a ReceivedNotification can be prevented to reach the displayedStream inside/during the createdStream?

I am aware that I can AwesomeNotifications().cancel(id) during the the createdStream phase but this wouldn't be applicable to my use case.

I am creating messaging notifications with the Inbox layout, and either I am using this wrong but if I don't use the same NotificationContent.ID, a separate notification/status bar icon will be created for each notification. Reusing the NotificationContent.ID properly nests the notifications inside one Inbox layout notification, but cancel(id) would remove all the nested messages.

Why? My application runs a background poller for updates in case Firebase fails to push the notification, and then I manually create a notification with AwesomeNotifications().createNotification(content: ...).

However sometimes Firebase would push a notifcation a great deal later when my background poller already created the notifcation in question, thus a duplicate would occur, and it is this duplicate that I wish to prevent from being displayed.

Thanks in advance,
Gilles

(awesome package indeed by the way!)

@efraespada
Copy link
Contributor

I need the feature too. By the moment I'm controlling the receiver presence for sending it or not. It could be interesting to control what is receiving in the notification and display it or not.

For example, a chat application receives a notification and shows it but, if the chat room (where the notification was generated) is opened, the notification shouldn’t be displayed.

@MrWaggel
Copy link
Author

@efraespada

For example, a chat application receives a notification and shows it but, if the chat room (where the notification was generated) is opened, the notification shouldn’t be displayed.

You can control this, the way I am doing it is (adjusted for your needs);

Create a global variable 'int chatroomActiveID = null';

In the widget that shows the chatroom screen; set the global variable in the 'initState()', and set it back to null in the 'dispose()'.

int chatRoomActiveID = null;
class ChatRoomState extends State<ChatRoom> {
    @override
    void initState() {
        ...
        chatRoomActiveID = x;
        super.initState();
    }

    @override
    void dispose() {
        ...
        chatRoomActiveID = null;
        super.dispose();
    }
}

Then import your chatRoom dart file (where the global variable is located) to the file which contains your 'createdStream.listen(...)' logic. Now in this logic

AwesomeNotifications().createdStream.listen((ReceivedNotification notification) {
    ...
    int chatRoomID = notification.payload["chatRoomID"];
    if (chatRoomID == chatRoomActiveID) {
        // Chatroom is already open, no need to show this notification
        AwesomeNotifications().cancel(notification.id);
        return;
    }
}

@rafaelsetragni
Copy link
Owner

rafaelsetragni commented Jan 20, 2021

I need the feature too. By the moment I'm controlling the receiver presence for sending it or not. It could be interesting to control what is receiving in the notification and display it or not.

For example, a chat application receives a notification and shows it but, if the chat room (where the notification was generated) is opened, the notification shouldn’t be displayed.

Try to use the properties displayOnForeground and displayOnBackground (bool) inside content field

@efraespada
Copy link
Contributor

@rafaelsetragni @MrWaggel

You can control this, the way I am doing it is (adjusted for your needs);

I've tested this option but only works in Android. In iOS the AwesomeNotifications().cancel(...); method doesn't cancel the received notification. The cancel method works perfectly but in this case, it doesn't avoid the notification display.

I've implemented it because at least in Android works fine.

Try to use the properties displayOnForeground and displayOnBackground (bool) inside content field

I've also implemented that solution controlling the OS. When sending the notification:

bool displayInForeground = device.os == "android";

In Android, the behavior is what I was looking for. In iOS, the notification doesn't appear when the application is opened.

Thanks for your work! 😄

@rafaelsetragni rafaelsetragni added the help wanted Extra attention is needed label Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants