简体   繁体   English

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

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

I need your help, all looks good but I cannot receive notification on my react-native application.我需要你的帮助,一切看起来都很好,但我无法在我的 react-native 应用程序上收到通知。

Actual setup:实际设置:

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

  • Background Modes: Background fetch and Remote notifications enabled后台模式:启用后台获取和远程通知
  • Push Notifications推送通知

Firebase Project: Firebase 项目:

  • APNs key was added, with KeyId, TeamId添加了 APNs 密钥,带有 KeyId、TeamId

Apple Developer:苹果开发者:

  • Identifier added with Push Notifications enabled添加了启用推送通知的标识符
  • Key added with "Apple Push Notifications service (APNs)"使用“Apple 推送通知服务 (APNs)”添加的密钥
  • Profiles > I created a "Provisioning Profile" to try if I have a connection issue(not resolved with this) Profiles > 我创建了一个“Provisioning Profile”来尝试如果我有连接问题(没有解决这个问题)

Good to know: I actually use the firebase authentification and it works so I don't think it's a GoogleService-Info.plist issue.很高兴知道:我实际上使用了 firebase 身份验证并且它有效,所以我认为这不是 GoogleService-Info.plist 问题。

App.js应用程序.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;
}, [])

Return:返回:

 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. 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. I tried to create a notification from REST我试图从 REST 创建一个通知

REST Request Setup: REST 请求设置:

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

Postman Result: Postman 结果:

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

React-native result: Nothing append on react-native app. React-native 结果: react-native 应用程序上没有 append。

Resolved:解决:

App.js应用程序.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.

相关问题 Firebase 云消息:无法在 React-js 中接收通知 - Firebase cloud messaging: Cannot receive notification in React-js 无法使用 React-Native-Firebase 接收通过 iOS 中的 Firebase 控制台发送的推送通知 - Unable to receive push notification send via Firebase console in iOS using React-Native-Firebase 如何使用 React Native Script 在后台接收 Firebase 通知 - How to receive Firebase Notification in background using React Native Script FCM iOS 推送通知收不到任何通知 - FCM iOS Push Notification cannot receive any notification 如何使用 react-native-firebase/messaging(FCM) 从栏点击通知后将应用程序打开到特定页面? - How to open app to specific page after click notification from bar by using react-native-firebase/messaging(FCM)? iOS 无法在 react-native-firebase 中接收通知 - iOS can't receive notifications in react-native-firebase 打开应用程序后如何删除 react-native-firebase/messaging 中的通知标记计数 - How to remove the notification badge count in react-native-firebase/messaging after opening the app React Native Firebase 推送通知 iOS 真实设备 - React Native Firebase Push Notification iOS Real Device React Native 通知图像未在 IOS 上显示 - React Native notification image doesn't showing on IOS 为什么我的 flutter 应用在 iOS 上运行时没有使用 Firebase 云消息接收通知? - Why my flutter app doesn't receive notifications using Firebase Cloud Messaging when running on iOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM