反应本机registerHeadlessTask不起作用-RNFirebaseBackgroundMessage

时间:2019-12-07 20:36:17

标签: android reactjs firebase react-native react-native-firebase

只要通知为,是否可以在应用程序的 AsyncStorage 保存react-native-firebase 远程推送通知>被接收(无论何时接收,都不会被点击)?当应用被杀死关闭时?我尝试使用该 headlesstask ,但没有执行任何操作。 (android)

我的代码: index.js

import {AppRegistry, AsyncStorage} from 'react-native';
import firebase from 'react-native-firebase';

import App from './App';
import {name as appName} from './app.json';
import { bgMessaging } from './bgMessaging'; 

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging)
AppRegistry.registerComponent(appName, () => App);

bgMessaging.js

import {AsyncStorage, Platform} from 'react-native';
import firebase from 'react-native-firebase';

export default async (message) => {

const channel = new firebase.notifications.Android.Channel('myapp-channel', 'myAPP Channel', 
                firebase.notifications.Android.Importance.Max).setDescription('myAPP Channel');

firebase.notifications().android.createChannel(channel);

const localNotification = new firebase.notifications.Notification().android
    .setChannelId('myapp-channel')
    .android.setSmallIcon('ic_stat_ic_notif')
    .android.setLargeIcon('ic_launcher_round')
    .android.setColor('#000000')
    .android.setPriority(firebase.notifications.Android.Priority.High)
    .setNotificationId(message._notificationId)
    .setSound('default')
    .setTitle(message._data.title)
    .setSubtitle(message._data.body)
    .setBody(message._data.body)
    .setData(message._data)

  firebase.notifications().displayNotification(localNotification).catch((err)=>console.log('err',err))

    //console.log(msg.data);

      let offerToBeSaved = { 
              "title" : message._data.title, 
              "body" : message._data.body, 
              "dateReceived" : message._data.dateReceived,
              "coupon": message._data.coupon 
            }

          let existingOffers = await AsyncStorage.getItem("offers");

          let newOffer = JSON.parse(existingOffers);

              if( !newOffer ){
                newOffer = []
              }

              newOffer.push( offerToBeSaved )

            await AsyncStorage.setItem("offers", JSON.stringify(newOffer) )
              .then( ()=>{
              console.log('saved offer')
              } )
              .catch( ()=>{
              console.log('error saving offer')
            } )


  return Promise.resolve()

}

从nodejs发送通知时的有效载荷:

let payload = {
     notification: {
        title: 'MYAPP',
        body: 'You just won $1M!!'
     },
    data: {
        title: 'You WON $1M !!!',
        body: 'can you imagine? lol - fake news',
        dateReceived: "moment() - a date"
        coupon: "123"
    }
}

收到通知时。它显示,但不执行bgMessaging代码。 如果有人可以帮助,不胜感激。我已经为此苦苦挣扎了好几天了。

0 个答案:

没有答案