Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not show specific notification React Native

I do not want to show "NEW_MSG" Push Notification type in a specific screen. I coded below code but when I test it I always get a notification. How can I fix it?

  useEffect(() => {
    notificationListener.current = Notifications.addNotificationReceivedListener(
      (notification) => {
        const {
          room_id,
          message_text,
          type,
        } = notification.request.content.data;
      
        
        // I do not want to show NEW_MSG,
         if (type == "NEW_MSG") {
           console.log(notification);
           return;
         }

      }
    );

    return () => {
      // Unsubscribe when component unmmounts
      Notifications.removeNotificationSubscription(
        notificationListener.current
      );
    };
  }, []);

Note: I use Expo Notifications

like image 756
Leo S Avatar asked Aug 09 '26 15:08

Leo S


2 Answers

I believe what you want to do is set up a single custom notification handler that refers to a state variable that you can set/unset on your special screen using the useEffect pa ttern you demonstrated above (i.e. the componentDidMount functional useEffect style).

Apologies, I didn't try writing this up myself first so it may need syntactic help. But it would go something like this...

Setup the handler function for your app and use it for handling your notifications

// The function must refer to a state variable that reacts to whether you're on the screen
let notificationHandlerFn = (notification: Notification) => {
  {
    shouldShowAlert: !onMySuperDuperScreen,   // special state variable
    shouldPlaySound: false,
    shouldSetBadge: false,
  }};

// Register the handler to use the function
Notifications.setNotificationHandler({
  handleNotification: notificationHandlerFn
});

Later, on your special screen

   // Use useEffect to control the state variable
useEffect(() => {
    onMySuperDuperScreen = true;

    return () => {
      onMySuperDuperScreen = false;
    };
  }, []);

Take a look at the expo docs here for details. Read in the section is titled "Handling incoming notifications when the app is in foreground".

like image 61
Atmas Avatar answered Aug 12 '26 04:08

Atmas


with this class (Android) you can overwrite the expo notification service and to decide show or not an push notification.

import expo.modules.notifications.notifications.JSONNotificationContentBuilder;
import expo.modules.notifications.notifications.interfaces.NotificationsScoper;
import expo.modules.notifications.notifications.model.Notification;
import expo.modules.notifications.notifications.model.NotificationContent;
import expo.modules.notifications.notifications.model.NotificationRequest;
import expo.modules.notifications.notifications.model.triggers.FirebaseNotificationTrigger;
import expo.modules.notifications.notifications.service.NotificationsHelper;
import expo.modules.notifications.tokens.interfaces.FirebaseTokenListener;

public class FirebaseMessageService extends FirebaseMessagingService {

@Override
  public void onCreate() {
    super.onCreate();
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    
    try {
        
        JSONObject json = new JSONObject(remoteMessage.getData());
        JSONObject body = new JSONObject(json.getString("body"));
        
        if (body...) {  //HERE YOUR CONDITIONS
            
            //NOT SHOW RETURN:
            return;
            
        }else{
            
            //SHOW EXPO NOTIFICATION
            createNotificationsHelper().notificationReceived(createNotification(remoteMessage));

        }
    }
    catch (Exception e){
        
    }   
}

public void sendRegistrationToServer(String token) {
}

protected NotificationsHelper createNotificationsHelper() {
  return new NotificationsHelper(this, NotificationsScoper.create(this).createReconstructor());
}

protected Notification createNotification(RemoteMessage remoteMessage) {
  String identifier = remoteMessage.getMessageId();
  if (identifier == null) {
    identifier = UUID.randomUUID().toString();
  }
  JSONObject payload = new JSONObject(remoteMessage.getData());
  NotificationContent content = new JSONNotificationContentBuilder(this).setPayload(payload).build();
  NotificationRequest request = createNotificationRequest(identifier, content, new FirebaseNotificationTrigger(remoteMessage));
  return new Notification(request, new Date(remoteMessage.getSentTime()));
}

protected NotificationRequest createNotificationRequest(String identifier, NotificationContent content, FirebaseNotificationTrigger notificationTrigger) {
  return new NotificationRequest(identifier, content, notificationTrigger);
}

}

in your manifest:

<service
    android:name=".FirebaseMessageService"
    android:exported="false">
    <intent-filter android:priority="-1">
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
  </service>
like image 42
Jorge Rivera Avatar answered Aug 12 '26 05:08

Jorge Rivera