Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Creating notification builder
- notificationBuilder = () => {
- // Creating channel for android 8.0.0 (API 26)
- const channel = new firebase.notifications.Android.Channel(
- "channelId",
- "channelId",
- firebase.notifications.Android.Importance.Max
- ).setDescription("Channel Description");
- firebase.notifications().android.createChannel(channel);
- firebase.messaging().subscribeToTopic("channel_topic");
- // the listener returns a function you can use to unsubscribe
- this.unsubscribeFromNotificationListener = firebase
- .notifications()
- .onNotification(notification => {
- // for Android Notification
- if (Platform.OS === "android") {
- const localNotification = new firebase.notifications.Notification(
- {
- sound: "default",
- show_in_foreground: true,
- }
- )
- .setNotificationId(notification.notificationId)
- .setTitle(notification.title)
- .setSubtitle(notification.subtitle)
- .setBody(notification.body)
- .setData(notification.data)
- .android.setChannelId("channelId") // e.g. the id you chose above
- .android.setSmallIcon("ic_stat_ic_notification") // set icon (must be samee with manifest)
- .android.setColor("#2196F3") // you can set a color here (hexcode must be same with manifest)
- .android.setAutoCancel(true)
- .android.setBigText(notification.body)
- .android.setPriority(
- firebase.notifications.Android.Priority.Default
- );
- //handle notification action if application is on foreground / background
- firebase
- .notifications()
- .onNotificationOpened(this.onNotificationPressed);
- // Displaying notification
- firebase
- .notifications()
- .displayNotification(localNotification)
- .catch(error =>
- console.error(
- "Home.js : " +
- "firebase.notifications Android error : " +
- error
- )
- );
- } else if (Platform.OS === "ios") {
- const localNotification = new firebase.notifications.Notification()
- .setNotificationId(notification.notificationId)
- .setTitle(notification.title)
- .setSubtitle(notification.subtitle)
- .setBody(notification.body)
- .setData(notification.data)
- .ios.setBadge(notification.ios.badge);
- firebase
- .notifications()
- .onNotificationOpened(this.onNotificationPressed);
- firebase
- .notifications()
- .displayNotification(localNotification)
- .catch(error =>
- console.error(
- "Home.js : " +
- "firebase.notifications IOS error : " +
- error
- )
- );
- }
- });
- this.handleInitialNotification();
- };
- //Handling initialNotification (opened notification when apps closed)
- async handleInitialNotification() {
- const oldNotification = await AsyncStorage.getItem("notification");
- console.log("Home.js : " + "oldNotifID -> " + oldNotification);
- //handle notification action If application is closed
- firebase
- .notifications()
- .getInitialNotification()
- .then(notificationOpen => {
- //action here
- })
- .catch(error =>
- console.log(
- "Home.js : " +
- "firebase.getInitialNotification error : " +
- error
- )
- );
- }
- //function for handling opened notification on foreground / background (not killed / closed)
- onNotificationPressed = notificationOpen => {
- if (notificationOpen) {
- // action here
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement