Javascript 不显示特定的通知
我不想在特定屏幕中显示“NEW_MSG”推送通知类型。我编写了下面的代码,但当我测试它时,我总是收到通知。我怎样才能修好它Javascript 不显示特定的通知,javascript,reactjs,react-native,push-notification,expo,Javascript,Reactjs,React Native,Push Notification,Expo,我不想在特定屏幕中显示“NEW_MSG”推送通知类型。我编写了下面的代码,但当我测试它时,我总是收到通知。我怎样才能修好它 useEffect(() => { notificationListener.current = Notifications.addNotificationReceivedListener( (notification) => { const { room_id, message_t
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
);
};
}, []);
注意:我使用Expo Notifications这个类(Android),您可以覆盖Expo Notifications服务并决定是否显示推送通知
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);
}
}
在您的舱单中:
<service
android:name=".FirebaseMessageService"
android:exported="false">
<intent-filter android:priority="-1">
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
我相信您要做的是设置一个自定义通知处理程序,该处理程序引用一个状态变量,您可以使用上面演示的Useffect模式(即componentDidMount functional Useffect样式)在特殊屏幕上设置/取消设置该状态变量 抱歉,我没有先尝试自己写这篇文章,所以可能需要语法帮助。但事情会是这样的 为您的应用程序设置处理程序功能,并使用它来处理通知
// 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
});
稍后,在您的特殊屏幕上
// Use useEffect to control the state variable
useEffect(() => {
onMySuperDuperScreen = true;
return () => {
onMySuperDuperScreen = false;
};
}, []);
详情请看世博会文件。阅读标题为“当应用程序位于前台时处理传入通知”的部分