簡體   English   中英

React native 無法在 ios 中使用 firecloud Messaging 接收通知

[英]React native cannot receive notification using firecloud Messaging in ios

我需要你的幫助,一切看起來都很好,但我無法在我的 react-native 應用程序上收到通知。

實際設置:

在 project.xcworkspace 中,TARGETS > {App Name} > Signing & Capabilities:

  • 后台模式:啟用后台獲取和遠程通知
  • 推送通知

Firebase 項目:

  • 添加了 APNs 密鑰,帶有 KeyId、TeamId

蘋果開發者:

  • 添加了啟用推送通知的標識符
  • 使用“Apple 推送通知服務 (APNs)”添加的密鑰
  • Profiles > 我創建了一個“Provisioning Profile”來嘗試如果我有連接問題(沒有解決這個問題)

很高興知道:我實際上使用了 firebase 身份驗證並且它有效,所以我認為這不是 GoogleService-Info.plist 問題。

應用程序.js

const requestNotificationPermission = async () => {
  messaging()
    .requestPermission()
    .then(authStatus => {
      console.log('APNs Status: ', authStatus);
      if (
        authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
        authStatus == messaging.AuthorizationStatus.PROVISIONAL
      ) {
        messaging()
          .getToken()
          .then(async (token) => {
            await messaging().registerDeviceForRemoteMessages()
              .then((result) => {
                console.log('Device registered for remote messages: ', result);
              });
            console.log('Token', token);
          });
        messaging().onTokenRefresh(token => {
          console.log('Refresh Token: ', token)
        });
      }
    })
    .catch(err => {
      console.log('Error: ', err);
    });
}

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

  return unsubscribe;
}, [])

返回:

 LOG  APNs Status:  1
 LOG  Device registered for remote messages:  true
 LOG  Token fmORQaQ0FUCjnGA1TgXSDi:APA91bFVDMvgkY13Rl-muzI2kOKCJauFcJCVF7sZZxgnuicawTIcvrl73JtNUEruobDfu1igvgVkXobi3gUTvGXD1QMZoOskXUzAEkDJpMgUvug-9KudE6bJl9oBL0tJCc9Eqv0GXfXa
  1. I tried to create a notification from https://console.firebase.google.com/u/0/project/proj-number/messaging Result: Nothing append on react-native app.

  2. 我試圖從 REST 創建一個通知

REST 請求設置:

{
    "data": {},
    "notification": {
        "body": "This is an FCM notification",
        "title": "From Postman"
    },
    "to": "fmORQaQ0FUCjnGA1TgX[...]"
}

Postman 結果:

{
    "multicast_id": 5211676236934629976,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "0:1660579924324918%fedf4cbefedf4cbe"
        }
    ]
}

React-native 結果: react-native 應用程序上沒有 append。

解決:

應用程序.js

const getToken = () => {
  messaging()
    .getToken()
    .then(x => console.log(x))
    .catch(e => console.log(e));
};
const registerForRemoteMessages = () => {
  messaging()
    .registerDeviceForRemoteMessages()
    .then(() => {
      console.log('Registered');
      requestPermissions();
    })
    .catch(e => console.log(e));
};
const requestPermissions = () => {
  messaging()
    .requestPermission()
    .then((status) => {
      if (status === 1) {
        console.log('Authorized');
        onMessage();
      } else {
        console.log('Not authorized');
      }
    })
    .catch(e => console.log(e));
};

const onMessage = () => {
  messaging()
    .onMessage(response => {
      console.log(response.data.notification);
    });
};


React.useEffect(() => {
  getToken();
  if (Platform.OS === 'ios') {
    registerForRemoteMessages();
  } else {
    onMessage();
  }
  const unsubscribe = messaging().onMessage(async remoteMessage => {
    Alert.alert('A new FCM message arrived!', JSON.stringify(remoteMessage));
  })

  return unsubscribe;
}, [])

暫無
暫無

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

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