【问题标题】:React Native Push Notifications with react-native-fcm使用 react-native-fcm 反应原生推送通知
【发布时间】:2017-03-03 02:36:19
【问题描述】:

I'm using this library.

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

我正在使用火力基地。我进入我的控制台 > 通知 > 发送消息 > 特定设备(我从下面的远程调试 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)

        });
});

【问题讨论】:

  • 我也有同样的问题...你有什么解决办法吗?
  • 您必须使用 FCM.presentLocalNotification()。因为如果您的应用程序处于活动状态,则通知处理由您负责,而如果它处于非活动状态,则手机会自动显示它

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


【解决方案1】:

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

“custom_notification”:{ "body": "测试体", "title": "测试标题", "颜色":"#00ACD4", “优先级”:“高”, "icon":"ic_notif", “组”:“组”, “身份证”:“身份证”, “show_in_foreground”:真 }

所以,我认为应用程序必须接收通知,解析出数据,并添加这个 custom_notification 参数。

【讨论】:

    【解决方案2】:

    在你的构造函数中如何:

    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');
    

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 2017-12-18
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多