gpt4 book ai didi

javascript - 不显示特定通知 React Native

转载 作者:行者123 更新时间:2023-12-04 07:44:52 25 4
gpt4 key购买 nike

我不想在特定屏幕中显示“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com