javascript - 不显示特定通知 React Native

标签 javascript reactjs react-native push-notification expo

我不想在特定屏幕中显示“NEW_MSG”推送通知类型。我编写了下面的代码,但是当我测试它时,我总是收到通知。我该如何解决?

  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
      );
    };
  }, []);
注意:我使用世博通知

最佳答案

使用此类(Android),您可以覆盖博览会通知服务并决定是否显示推送通知。

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);
}
}
在您的 list 中:
<service
    android:name=".FirebaseMessageService"
    android:exported="false">
    <intent-filter android:priority="-1">
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
  </service>

关于javascript - 不显示特定通知 React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67246728/

相关文章:

javascript - 如何在 React 中动态更改按钮大小和颜色

react-native - ReactNative :Failed to parse React Native CLI configuration: groovy. json.JsonException:无法确定当前字符

ios - 如何在 React Native 应用程序上保证我的 api key 安全

Javascript如何使函数/变量全局可用

javascript - 当页面滚动时隐藏元素,但当页面变为静态时元素再次出现

javascript - Bootstrap 3 模态 : `data-remote` loading image

javascript - 使用 Vue.js 获取所有选中复选框的列表

reactjs - 自动对焦在 React 中不起作用

javascript - 通过 JSX 在 React State 上使用来自异步函数的数据

react-native - 如何在 React Native 中刷新 Web View ?