Advertisement
armadiazrino

React Native FCM Notification Builder Snippets

Jun 28th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  //Creating notification builder
  2.      notificationBuilder = () => {
  3.          // Creating channel for android 8.0.0 (API 26)
  4.         const channel = new firebase.notifications.Android.Channel(
  5.             "channelId",
  6.             "channelId",
  7.             firebase.notifications.Android.Importance.Max
  8.         ).setDescription("Channel Description");
  9.         firebase.notifications().android.createChannel(channel);
  10.         firebase.messaging().subscribeToTopic("channel_topic");
  11.  
  12.         // the listener returns a function you can use to unsubscribe
  13.         this.unsubscribeFromNotificationListener = firebase
  14.             .notifications()
  15.             .onNotification(notification => {
  16.                 // for Android Notification
  17.                 if (Platform.OS === "android") {
  18.                     const localNotification = new firebase.notifications.Notification(
  19.                         {
  20.                             sound: "default",
  21.                             show_in_foreground: true,
  22.                         }
  23.                     )
  24.                         .setNotificationId(notification.notificationId)
  25.                         .setTitle(notification.title)
  26.                         .setSubtitle(notification.subtitle)
  27.                         .setBody(notification.body)
  28.                         .setData(notification.data)
  29.                         .android.setChannelId("channelId") // e.g. the id you chose above
  30.                         .android.setSmallIcon("ic_stat_ic_notification") // set icon (must be samee with manifest)
  31.                         .android.setColor("#2196F3") // you can set a color here (hexcode must be same with manifest)
  32.                         .android.setAutoCancel(true)
  33.                         .android.setBigText(notification.body)
  34.                         .android.setPriority(
  35.                             firebase.notifications.Android.Priority.Default
  36.                         );
  37.  
  38.                     //handle notification action if application is on foreground / background
  39.                     firebase
  40.                         .notifications()
  41.                         .onNotificationOpened(this.onNotificationPressed);
  42.  
  43.                     // Displaying notification
  44.                     firebase
  45.                         .notifications()
  46.                         .displayNotification(localNotification)
  47.                         .catch(error =>
  48.                             console.error(
  49.                                 "Home.js : " +
  50.                                     "firebase.notifications Android error : " +
  51.                                     error
  52.                             )
  53.                         );
  54.                 } else if (Platform.OS === "ios") {
  55.                     const localNotification = new firebase.notifications.Notification()
  56.                         .setNotificationId(notification.notificationId)
  57.                         .setTitle(notification.title)
  58.                         .setSubtitle(notification.subtitle)
  59.                         .setBody(notification.body)
  60.                         .setData(notification.data)
  61.                         .ios.setBadge(notification.ios.badge);
  62.  
  63.                     firebase
  64.                         .notifications()
  65.                         .onNotificationOpened(this.onNotificationPressed);
  66.                     firebase
  67.                         .notifications()
  68.                         .displayNotification(localNotification)
  69.                         .catch(error =>
  70.                             console.error(
  71.                                 "Home.js : " +
  72.                                     "firebase.notifications IOS error : " +
  73.                                     error
  74.                             )
  75.                         );
  76.                 }
  77.             });
  78.         this.handleInitialNotification();
  79.     };
  80.  
  81.      //Handling initialNotification (opened notification when apps closed)
  82.      async handleInitialNotification() {
  83.         const oldNotification = await AsyncStorage.getItem("notification");
  84.         console.log("Home.js : " + "oldNotifID -> " + oldNotification);
  85.  
  86.         //handle notification action If application is closed
  87.         firebase
  88.             .notifications()
  89.             .getInitialNotification()
  90.             .then(notificationOpen => {
  91.                //action here
  92.             })
  93.             .catch(error =>
  94.                 console.log(
  95.                     "Home.js : " +
  96.                         "firebase.getInitialNotification error : " +
  97.                         error
  98.                 )
  99.             );
  100.     }
  101.  
  102.     //function for handling opened notification on foreground / background (not killed / closed)
  103.     onNotificationPressed = notificationOpen => {
  104.         if (notificationOpen) {
  105.            // action here
  106.         }
  107.     };
  108.  
  109.    
  110.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement