0

我做了以下更改:

如何使用 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做这件事。

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

4

3 回答 3

0

当应用程序处于前台时,React native 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;
  }, []);
于 2022-01-07T11:20:39.673 回答
0

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

于 2022-01-14T14:19:14.003 回答
0

第一,您需要获得消息许可

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);
}, []); 
于 2022-01-07T11:36:41.100 回答