后台React-Native-Firebase通知(Android)

时间:2019-10-10 13:43:49

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

我正在使用react-native-firebase库,并且正在集思广益以在后台运行时自定义通知,我尝试遵循文档,但无济于事,是否有本机代码.java处理通知? / p>

1 个答案:

答案 0 :(得分:0)

要自定义后台通知,您可以在应用程序的索引中添加:

import { AppRegistry } from 'react-native'
import App from './App'
import { backgroundMessageNotificationHandler } from 'src/common/notifications'

AppRegistry.registerComponent('testApp', () => App)
AppRegistry.registerHeadlessTask(
  'RNFirebaseBackgroundMessage',
  () => backgroundMessageNotificationHandler)

backgroundMessageNotificationHandler可能是这样的:

export const backgroundMessageNotificationHandler = async (message: any) => {
  const localNotification = new firebase.notifications.Notification().android
    .setChannelId(androidNotificationsChannel.channelId)
    .android.setSmallIcon('iconBlackAndWhite')
    .android.setLargeIcon('iconBlackAndWhite')
    .android.setPriority(firebase.notifications.Android.Priority.High)
    .setNotificationId(message.messageId)
    .setSound('default')
    .setTitle(message.data.title)
    .setBody(message.data.body)
    .setData(message.data)

  if (Platform.OS === 'android') {
    createChannel()
  }

  firebase.notifications().displayNotification(localNotification)

  return Promise.resolve()
}

请记住遵循设置build.gralde,MainActivity和AndroidManifest的步骤。您可以按照以下帖子中指定的步骤进行操作:

React-Native Android Push Notification via Firebase