簡體   English   中英

如何使用 react-native-firebase/messaging(FCM) 從欄點擊通知后將應用程序打開到特定頁面?

[英]How to open app to specific page after click notification from bar by using react-native-firebase/messaging(FCM)?

有什么方法可以在點擊通知后使用 react-native-firebase/messaging(FCM) 打開應用程序到特定頁面?

我沒有從文檔中找到信息。

@react-native-firebase 版本:

    "@react-native-firebase/app": "^15.3.0",
    "@react-native-firebase/messaging": "^15.3.0",

代碼只是:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow strict-local
 */

import React, {useEffect} from 'react';
import {Alert, StyleSheet, Text, View} from 'react-native';

import messaging from '@react-native-firebase/messaging';
import styles from '../css/styles';

let fcmUnsubscribe = null;

function Notify(props) {
  useEffect(() => {
    const unsubscribe = messaging().onMessage(async remoteMessage => {
      Alert.alert('A new FCM message arrived!', JSON.stringify(remoteMessage));
    });

    return unsubscribe;
  }, []);
  return (
    <View style={styles.sectionContainer}>
      <Text style={styles.highlight}>{props.test}</Text>
    </View>
  );
}

export default Notify;

關於如何在我的應用程序中提升 fcm 功能,我使用了一種非標准方法。 您有 remoteMessage ,您可以通過它發送有效負載以進行通知...好吧,在此有效負載中,我放置了應用程序所需的所有內容,並將其與 | 分開例如。 所以我的 remoteMessage 字符串是這樣的:

顯示給用戶的一些文本通知|頁面打開|部分頁面位置|需要其他自定義數據

然后在通知警告框中顯示這個之前,我首先將它解析為數組或幾個變量,然后只將與通知文本相關的部分字符串發送到顯示,其他變量用於打開特定頁面或其他一些東西。

像這樣的東西(這是 JS,因為它是第一次復制你,但類似的適用於 RN)

messaging.onBackgroundMessage(function(payload) {
    var notificationTitle = payload.data.title;
    var notificationBody = payload.data.body.substring(0, payload.data.body.indexOf("|"));
    var notificationURL = payload.data.body.split("|")[1];
    var notificationStatusURL = payload.data.body.split("|")[2];

你可以像下面這樣

useEffect(() => {
    const unsubscribe = messaging().onMessage(async remoteMessage => {
      Alert.alert('A new FCM message arrived!', JSON.stringify(remoteMessage));
if (remoteMessage?.data?.screen_name) {
        navigation.navigate(remoteMessage?.data?.screen_name)
    };
    });

    return unsubscribe;
  }, []);

注意請檢查您的 function 是否包含在導航容器中,否則您可以使用 useNavigation 掛鈎

並使用您從 firebase 發送的參數名稱檢查 screen_name 參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM