简体   繁体   中英

React-native-firebase notification in background (Android)

I am using the react-native-firebase library and I am brainstorming to customize my notifications when they are in the background, I tried to follow the documentation but to no avail, are there native code.java to handle notifications?

To customize the background notifications you can add in the index of your application:

 import { AppRegistry } from 'react-native' import App from './App' import { backgroundMessageNotificationHandler } from 'src/common/notifications' AppRegistry.registerComponent('testApp', () => App) AppRegistry.registerHeadlessTask( 'RNFirebaseBackgroundMessage', () => backgroundMessageNotificationHandler)

The backgroundMessageNotificationHandler could be something like that:

 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() }

Remember to follow the steps to setting up the build.gralde, MainActivity and AndroidManifest. You can follow the steps specified in the following post:

React-Native Android Push Notification via Firebase

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM