不要显示特定通知React Native



我不想显示"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
);
};
}, []);

注意:我使用Expo Notifications

我相信您想要做的是设置一个单独的自定义通知处理程序,该处理程序引用一个状态变量,您可以使用上面演示的useEffect模式(即componentDidMount函数useEffect样式(在特殊屏幕上设置/取消设置该状态变量。

抱歉,我没有试着自己先写出来,所以它可能需要语法帮助。但事情会变成这样。。。

为您的应用程序设置处理程序功能,并使用它来处理您的通知

// 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;
};
}, []);

有关详细信息,请查看此处的世博文档。阅读部分的标题是";当应用程序处于前台时处理传入通知";。

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

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>

这是我的解决方案-您可以检查通知数据,确定是否要显示警报并播放警报声音。这是我的代码:

// https://docs.expo.dev/versions/latest/sdk/notifications/#handling-incoming-notifications-when-the-app-is-in-foreground
Notifications.setNotificationHandler({
handleNotification: async (notification) => {
let shouldShow;
// will not show alert and sound if we already in this chat
const chatId = notification?.request?.content?.data?.chatId;
const currentRouteNameIsChat = navigationRef.getCurrentRoute().name === "Chat";
const currentRouteChatIdIsSame = navigationRef.getCurrentRoute().params?.chatId == chatId;
if (chatId && currentRouteNameIsChat && currentRouteChatIdIsSame) {
shouldShow = false;
} else {
shouldShow = true;
}
return {
shouldShowAlert: shouldShow,
shouldPlaySound: shouldShow,
shouldSetBadge: false,
};
},
});

setNotificationHandler文档:https://docs.expo.dev/versions/latest/sdk/notifications/#setnotificationhandlerhandler-notificationhandler--空-空

最新更新