我正在使用react-native-firebase库,并且正在集思广益以在后台运行时自定义通知,我尝试遵循文档,但无济于事,是否有本机代码.java处理通知? / p>
答案 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的步骤。您可以按照以下帖子中指定的步骤进行操作: