【问题标题】:React-native with Firebase FCM not working使用 Firebase FCM 的 React-native 不起作用
【发布时间】:2022-01-07 09:45:13
【问题描述】:

我做了以下改动:

如何发送通知,使用 firebase-admin 并获得成功结果:

admin.messaging().sendToDevice(
        "token as string",
        {
        notification: {
            title: 'title here',
            body: 'body here'
            }
        },
        {
            priority: "high",
            contentAvailable: true,
            timeToLive: 2419200
        });

React-native 代码,这是当应用程序在前台时:

useEffect(() => {
    checkToken().then((token) => {
      console.log(token);
      // write to db
    });
    const unsubscribe = messaging().onMessage(async (remoteMessage) => {
      console.log(remoteMessage);
    });
    return unsubscribe;
  }, []);

我在 ios 文件夹中有 GoogleService-Info.plist 文件,我已经在 firebase 控制台 + teamid + appid 中上传了 APN。

随着 AppDelegate.m 的变化:

#import <Firebase.h>
....
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

[FIRApp configure];

当应用程序在前台运行时我没有收到任何通知,我发送的方式一定有问题(可能我有错误的选项)或者,我在 react-native 中的配置错误,不是当然。我现在只为IOS做这个。

任何信息或解决方案都会有所帮助。

【问题讨论】:

  • 你能检查一下PushNotification.onNotification=(message)=> {},从'react-native-push-notification'导入PushNotification;
  • 后台消息是否有效?确保您遵循此rnfirebase.io 和此rnfirebase.io/messaging/usage 和此rnfirebase.io/messaging/usage/ios-setup 的每一步。还要确保在真实设备上测试消息,因为 fcm 永远不会在 Ios 模拟器上工作,如文档中所述。当我第一次这样做时,我发现这也很难集成。
  • 谢谢您,我已按照上面的文档进行操作,直到现在才能正常工作。我会继续努力的。

标签: firebase react-native push-notification firebase-cloud-messaging react-native-fcm


【解决方案1】:

当应用程序处于前台时,React 本机 Firebase 不显示通知,所以我所做的是,我使用不同的库来处理前台通知。我正在使用 react-native-push-notification。\

适用于安卓

import PushNotification from 'react-native-push-notification';
import PushNotificationIos from '@react-native-community/push-notification-ios';

useEffect(() => {
    checkToken().then((token) => {
      console.log(token);
      // write to db
    });
    const unsubscribe = messaging().onMessage(async (remoteMessage) => {
      PushNotification.localNotification({
        message: remoteMessage.notification.body,
        title: remoteMessage.notification.title,
        bigPictureUrl: remoteMessage.notification.android.imageUrl,
        smallIcon: remoteMessage.notification.android.imageUrl,
      });
    });
    return unsubscribe;
  }, []);

对于 iOS: 对于 iOS,您必须安装 @react-native-community/push-notification-ios
还要按照文档中的建议执行所有本机安装步骤。 然后编写如下代码

PushNotification.configure({
    onNotification: (notification) => {
      if (notification) {
        console.log(notification);
      }
    },
  });

  useEffect(() => {
    // To display notification when app is in foreground
    const unsubscribe = messaging().onMessage(async (remoteMessage) => {
      PushNotificationIos.addNotificationRequest({
        message: remoteMessage.notification.body,
        title: remoteMessage.notification.title,
        bigPictureUrl: remoteMessage.notification.android.imageUrl,
        smallIcon: remoteMessage.notification.android.imageUrl,
      });
    });

    return unsubscribe;
  }, []);

【讨论】:

  • 我知道使用 firebase 我无法显示通知,我为那个 Notifee 使用了不同的包,对我来说问题是我在这里没有收到消息:messaging().onMessage(async (remoteMessage)I没有收到remoteMessage 参数中的消息。
  • android 和 ios 都会出现这种情况吗?
  • 我没有在android上测试过,只在ios上测试过。
  • 你能添加你从后端发送的通知对象吗?
  • 我正在使用 admin-firebase,对象在问题中,这里:{ notification: { title: 'title here', body: 'body here' } }
【解决方案2】:

首先你需要获得消息权限

async function requestUserPermission() {
    const authStatus = await messaging().requestPermission();
    const enabled =
      authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
      authStatus === messaging.AuthorizationStatus.PROVISIONAL;
  
    if (enabled) {
      console.log('Authorization status:', authStatus);
    }
}

然后你需要创建一个频道:

const createFCMChannel = async () => {
    const channelId = await notifee.createChannel({
      id: 'default',
      name: 'Default Channel',
    });
};

你可以直接调用这个方法,它会从notifee调用“displayNotification”

function onMessageReceived(message) {
    notifee.displayNotification(message.notification);
}

您可以在应用启动时在 useEffect 中使用这些方法:

useEffect(() => {
    createFCMChannel();
}, []);

useEffect(() => {
    requestUserPermission();
    messaging().onMessage(onMessageReceived);
    messaging().setBackgroundMessageHandler(onMessageReceived);
}, []); 

【讨论】:

  • 我知道这一点,对我来说问题是我没有收到消息,我知道 Notifee 是如何工作的。不过谢谢你。
【解决方案3】:

您可以直接从 AppDelegate 文件控制和打印通知数据。当您绕过应用程序逻辑并在第一个入口处处理数据时,它允许更快的调试过程。这样,您将找出问题所在 - 服务器端(通知数据结构等)或应用程序逻辑/本地处理过程。 只需在 Appdelegate.m 文件中使用此示例代码部分即可:

Messaging.messaging().appDidReceiveMessage(userInfo) 

  With swizzling disabled you must let Messaging know about the message, for Analytics
  [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
  FIRMessaging.messaging().appDidReceiveMessage(userInfo);
  Messaging.messaging().delegate = self
        completionHandler(.NoData)
          [[FIRMessaging messaging] appDidReceiveMessage:userInfo];

  ...

  // Print full message.
  NSLog(@"%@", userInfo);

根据经验,请从 Apple 开发者页面查看有关结构和参数的信息:https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622921-application

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多