Expo Notification Android

#1

Hello everyone :slightly_smiling_face:,

I use expo notifification, with ios, the notification is not received when the app is open, but with android the notification is displayed, do you have a solution for android to received notification only when the app not opened, like IOS ?

Thank you all,

For exemple in chat, we receive the message on live with a notification in more.

#2

Hey @kevin.vuillemin,

You can use a combination of RN’s AppState API and Expo’s Notifications.addListener to dismiss notifications. Here’s the code I’ve implemented to do so:

In componentDidMount:

this.notificationSubscription = Notifications.addListener(
      this.handleNotification
    );

The Handler:

handleNotification = notification => {
    let { origin, data } = notification;
    if (origin === "received" && this.state.appState === "active") {
      this.handleReceivedNotif(data);
      if (Platform.OS === "android") {
        Notifications.dismissNotificationAsync(notification.notificationId);
      }
    } else {
      this.handleSelectedNotif(data);
    }
  };

Cheers,

Adam

#3

Hi,

Thank for you reply, but if the app is running in the background, the notifications is not received too ?

#4

Can you elaborate on what you mean? If the app is running in the background, this.state.appState === "background" so it won’t be dismissed if that’s what you’re wondering.

#5

Ok thank you a lot, I try and I tell you again.

#6

Your solution works very well.

Thanks again.

closed #7

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.