在React Native iOS中显示自定义FCM推送通知

3
我是一位有用的助手,可以翻译文本。
我正在开发一个React Native项目,想要接收FCM推送通知。我正在使用react-native-fcm模块。我想做的是手动显示通知。
在这个模块中,有一个函数。
import {Platform} from 'react-native';
import FCM, {FCMEvent, RemoteNotificationResult, WillPresentNotificationResult, NotificationType} from 'react-native-fcm';

FCM.on(FCMEvent.Notification, async (notif) => {
  console.log(notif);
});

在这里,'notif' 应该是设备接收到的消息。然而,当通知被发送到设备时,console.log(notif); 从未被调用,我无法接收。

我想要做的是通过手动处理 notif json 来使用以下函数显示通知。

FCM.presentLocalNotification({
        id: "UNIQ_ID_STRING",                               // (optional for instant notification)
        title: "My Notification Title",                     // as FCM payload
        body: "My Notification Message",                    // as FCM payload (required)
        sound: "default",                                   // as FCM payload
        priority: "high",                                   // as FCM payload
        click_action: "ACTION",                             // as FCM payload
        badge: 10,                                          // as FCM payload IOS only, set 0 to clear badges
        number: 10,                                         // Android only
        ticker: "My Notification Ticker",                   // Android only
        auto_cancel: true,                                  // Android only (default true)
        large_icon: "ic_launcher",                           // Android only
        icon: "ic_launcher",                                // as FCM payload, you can relace this with custom icon you put in mipmap
    });

我还意识到,发送给 FCM 的消息也会影响模块显示消息的方式。更多详情请查看 FCM
我猜想我的 FCM 设置应该是正确的,因为我能够接收通知。如果我使用键 "notification" 发送消息,则我的 React Native 应用程序可以接收到通知。例如:
"notification":{
  "title":"Portugal vs. Denmark",
  "body":"great match!"
}

但之前的函数中的console.log(notif);仍未被调用。

我还尝试使用负载发送通知

"data" : {
  "title" : "Mario",
  "body" : "PortugalVSDenmark"
}

但是console.log(notif);仍未被调用。

有人知道react-native-fcm和firebase-cloud-messaging的工作机制吗?

非常感谢!

1个回答

0
请使用以下代码获取 FCM 令牌,并将其注册到 Firebase。成功注册后,您将在“notif”中接收远程通知。
FCM.on(FCMEvent.RefreshToken, (token) => {
    console.log("registration token:" + token);
// fcm token may not be available on first load, catch it here

});


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接