首页 文章

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

提问于
浏览
4

我正在开发一个想要接收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
    

    });

相关问题