使用react-native-fcm反应本机推送通知

时间:2017-03-03 02:36:19

标签: android firebase react-native firebase-cloud-messaging firebase-notifications

I'm using this library.

我试图在我的Nexus 5(android 6.0.1)上的通知托盘中显示推送通知。使用React Native 0.42,React Native CLI 2.0.1。我正在开发Ubuntu 14.04。

我正在使用firebase。我进入我的控制台>通知>发送消息>特定设备(我从下面的远程调试console.log获得)。

我正在记录通知,正如您在代码中看到的那样,他们确实可以访问我的设备,因为我可以在日志中看到它们。

但是,我不知道如何在通知栏中显示它们。浏览文档和搜索论坛,它们似乎应默认显示。

componentDidMount() {
        FCM.requestPermissions(); // for iOS
        FCM.getFCMToken().then(token => {
            console.log(token)
            // store fcm token in your server
        });
        this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
          console.log(notif)

        });
});

2 个答案:

答案 0 :(得分:0)

似乎需要“custom_notification”才能在顶部托盘中显示通知。我把它添加到我的有效载荷中:

“custom_notification”:{       “身体”:“测试身体”,       “标题”:“测试题目”,       “颜色”: “#00ACD4”       “优先级”:“高”,       “图标”:“ic_notif”       “group”:“GROUP”,       “id”:“id”,       “show_in_foreground”:是的     }

因此,我认为应用必须接收通知,解析数据并添加此custom_notification参数。

答案 1 :(得分:0)

构造函数中的以下内容如何:

FCM.requestPermissions(); // for iOS
FCM.getFCMToken().then(token => {
  console.log(token)
  // store fcm token in your server
});

this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
  // do some component related stuff
  console.log(notif);
  //alert(notif.fcm.body);

  FCM.presentLocalNotification({
    id: "UNIQ_ID_STRING",                               // (optional for instant notification)
    title: "My Notification Title",                     // as FCM payload
    body: notif.fcm.body,                    // 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
    big_text: "Show when notification is expanded",     // Android only
    sub_text: "This is a subText",                      // Android only
    color: "red",                                       // Android only
    vibrate: 300,                                       // Android only default: 300, no vibration if you pass null
    tag: 'some_tag',                                    // Android only
    group: "group",                                     // Android only
    picture: "https://google.png",                      // Android only bigPicture style
    ongoing: true,                                      // Android only
    my_custom_data: 'my_custom_field_value',             // extra data you want to throw
    lights: true,                                       // Android only, LED blinking (default false)
    show_in_foreground: true                                  // notification when app is in foreground (local & remote)
  });
});

FCM.subscribeToTopic('test_topic');