不显示特定通知 React Native
Posted
技术标签:
【中文标题】不显示特定通知 React Native【英文标题】:Do not show specific notification React Native 【发布时间】:2021-04-24 19:34:04 【问题描述】:我不想在特定屏幕中显示“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
【问题讨论】:
【参考方案1】:使用此类 (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>
【讨论】:
【参考方案2】:我相信您想要做的是设置一个自定义通知处理程序,该处理程序引用一个状态变量,您可以使用上面演示的 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;
;
, []);
查看博览会文档here 了解详情。阅读标题为“在应用程序处于前台时处理传入通知”的部分。
【讨论】:
以上是关于不显示特定通知 React Native的主要内容,如果未能解决你的问题,请参考以下文章
React Native Router Flux 选项卡导航不显示特定组件的选项卡图标
React-native:使用 zo0r/react-native-push-notification 显示前台通知,例如后台通知
React Native - 如何在 React Native 应用程序中使用 WebView 显示我的网站通知(适用于 Android)
当 React Native IOS App 中的 FlatList 为空时,ListEmptyComponent 不显示消息