android - React-native-firebase 后台通知(Android)
问题描述
我正在使用 react-native-firebase 库,并且正在集思广益以在后台自定义通知时,我尝试遵循文档但无济于事,是否有本机代码 .java 来处理通知?
解决方案
要自定义后台通知,您可以在应用程序的索引中添加:
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。您可以按照以下帖子中指定的步骤进行操作:
推荐阅读
- kotlin - 如何在 Kotlin 中优化 HashMap 的工作?
- sql - 加入 3 个 csv 文件
- javascript - SVG 图像不在浏览器中呈现,除了 ie
- javascript - 根据其大小将空间划分为给定数量的单元格
- angular - 托管在 AWS 重定向上的 Angular 应用程序在 S3 之前拒绝访问云端
- c# - 执行自定义 MS CRM 8.2 工作流操作后出现不可能的结果
- c++ - 在类中使用后的 C++ 枚举声明
- node.js - React Native(Expo)将图像发送到节点(数据库Postgres)
- python - 当 array2 得分小于 10 时,求 array1 得分的平均值
- javascript - 使用 React 的基于 API 的搜索