【问题标题】:React-native-firebase notification in background (Android)React-native-firebase 后台通知(Android)
【发布时间】:2019-10-10 13:43:49
【问题描述】:

我正在使用 react-native-firebase 库,我正在集思广益来自定义我的后台通知,我尝试按照文档进行操作但无济于事,是否有本地代码 .java 来处理通知?

【问题讨论】:

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


    【解决方案1】:

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

    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.grade、MainActivity 和 AndroidManifest。您可以按照以下帖子中指定的步骤进行操作:

    React-Native Android Push Notification via Firebase

    【讨论】:

    • 感谢您的帮助,我添加了创建频道的方法,但仍然不成功,当我发送通知时标题和正文仍然保持不变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多