React-native与Firebase FCM不工作

mdfafbf1  于 2023-05-01  发布在  React
关注(0)|答案(5)|浏览(161)

我做了以下更改:
如何发送通知,使用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;
  }, []);

我有GoogleService-Info。在ios文件夹中的plist文件,我已经在firebase控制台+ teamid + appid中上传了APNs。
AppDelegate中的更改。男:

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

[FIRApp configure];

当应用程序在前台运行时,我没有收到任何通知,我发送的方式一定有问题(可能是我的选项错误),或者我在react-native中的配置错误,不确定。我现在只想为iOS做这件事。
任何信息或解决方案将是有益的。

bvjxkvbb

bvjxkvbb1#

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

Android版

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;
  }, []);
5anewei6

5anewei62#

第一,你需要获得消息权限

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);
}, []);
6psbrbz9

6psbrbz93#

您可以直接从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

bqjvbblv

bqjvbblv4#

我最终通过使用实际设备运行干净运行来修复它。

txu3uszq

txu3uszq5#

有几件事可以解决这个问题
1.可以检查通知员权限是否被授予?
1.检查是否设置了前台和后台通知?
1.卸载应用程序并重新安装。
1.使用真实的器械清洁运行。

相关问题