{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/push-notification-in-react-native-with-firebase/",
    "result": {"data":{"site":{"siteMetadata":{"title":"CrewCode Solutions"}},"markdownRemark":{"id":"52705409-b4e8-54e0-8eff-9a29593793f8","excerpt":"In this article i will guide you step by step how you can implement firebase push notification in react native, android application Step 1: Create a new…","html":"<p>In this article i will guide you step by step how you can implement firebase push notification in react native, android application</p>\n<h4>Step 1: Create a new firebase project</h4>\n<p>Go to <a href=\"https://firebase.google.com/\">https://firebase.google.com/</a> and login to your firebase account and click on Add project</p>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Firebase-push-notification-in-react-native-1.png\" alt=\"addproject\"></p>\n<h4>Step 2: Add Android app on firebase project created previously</h4>\n<ul>\n<li>\n<p>In order to add firebase to your react native android application you need to add android app in your firebase project which you created previously</p>\n</li>\n<li>\n<p>For this go to Project Overview -> Project settings and then click on Add Android App</p>\n</li>\n</ul>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Firebase-push-notification-in-react-native-2.png\" alt=\"add-app-to-firebase\"></p>\n<ul>\n<li>\n<p>Go to AndroidManifest.xml file and grab the android package name</p>\n</li>\n<li>\n<p>In order to add android app you need to have SHA-1 key in order to generate this key you need go to your project directory and run this command</p>\n</li>\n</ul>\n<pre><code class=\"language-js\">keytool -list -v -keystore ./android/app/debug.keystore -alias androiddebugkey -storepass android -keypass android\n</code></pre>\n<p>You will get your SHA-1 key just copy and paste it in the field where it is required</p>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Firebase-push-notification-in-react-native-3.png\" alt=\"add-sha1\"></p>\n<ul>\n<li>Download the google-services.json file paste it into android/app</li>\n</ul>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Firebase-push-notification-in-react-native-4.png\" alt=\"download-google-service.json\"></p>\n<h4>Step 3: Install and add dependencies for Recieving Push notification in Android</h4>\n<ul>\n<li>Install required firebase messaging package</li>\n</ul>\n<pre><code class=\"language-js\">npm install --save @react-native-firebase/app\nnpm install --save @react-native-firebase/messaging\n</code></pre>\n<ul>\n<li>Under Root-level (project-level) Gradle file (<project>/build.gradle):</li>\n</ul>\n<pre><code class=\"language-js\">buildscript {\n  repositories {\n    // Make sure that you have the following two repositories\n    google()  // Google's Maven repository\n\n    mavenCentral()  // Maven Central repository\n\n  }\n  dependencies {\n    ...\n    // Add the dependency for the Google services Gradle plugin\n    classpath 'com.google.gms:google-services:4.3.13'\n\n  }\n}\n\nallprojects {\n  ...\n  repositories {\n    // Make sure that you have the following two repositories\n    google()  // Google's Maven repository\n\n    mavenCentral()  // Maven Central repository\n\n  }\n}\n</code></pre>\n<ul>\n<li>Module (app-level) Gradle file (<project>/<app-module>/build.gradle):</li>\n</ul>\n<pre><code class=\"language-js\">plugins {\n  id 'com.android.application'\n\n  // Add the Google services Gradle plugin\n  id 'com.google.gms.google-services'\n\n  ...\n}\n\ndependencies {\n  // Import the Firebase BoM\n  implementation platform('com.google.firebase:firebase-bom:30.4.1')\n\n\n  // TODO: Add the dependencies for Firebase products you want to use\n  // When using the BoM, don't specify versions in Firebase dependencies\n  // https://firebase.google.com/docs/android/setup#available-libraries\n}\n</code></pre>\n<h4>Step 4: Adding push notification code to listen notification in Foreground, Background</h4>\n<pre><code class=\"language-js\">import messaging from \"@react-native-firebase/messaging\";\n\nconst App = () => {\n  useEffect(() => {\n    //To handle background messaging\n    messaging().setBackgroundMessageHandler(async (remoteMessage) => {\n      let message_body = remoteMessage.notification.body;\n      let message_title = remoteMessage.notification.title;\n      let avatar = remoteMessage.notification.android.imageUrl;\n      Alert.alert(message_title, message_body);\n    });\n\n    //To handle foreground messaging\n    const subscribe = messaging().onMessage(async (remoteMessage) => {\n      let message_body = remoteMessage.notification.body;\n      let message_title = remoteMessage.notification.title;\n      let avatar = remoteMessage.notification.android.imageUrl;\n      Alert.alert(message_title, message_body);\n    });\n    return subscribe;\n  }, []);\n};\n</code></pre>\n<h4>Step 5: Testing the notification from firebase</h4>\n<p>To test this, go to the Firebase console of the application you created in the previous step. On the left sidebar, under <strong><em>Engage</em></strong>, click <strong><em>Cloud messaging</em></strong>. Then click the button <strong><em>Send your first message.</em></strong></p>\n<p>On the form:</p>\n<ul>\n<li>Enter any title (e.g., “test”) Under <strong><em>Notification text</em></strong></li>\n<li>Type in “Hello there!” under <strong><em>Notification image</em></strong></li>\n<li>Paste in any image address you’d like</li>\n<li>Click the Next button below</li>\n</ul>\n<p>Now, under <strong><em>Target</em></strong>, click <strong><em>Select an app</em></strong> and then select your app. Click <strong><em>Next</em></strong> on the next step and then <strong><em>Review</em></strong>. On the resulting popup, <strong><em>click Publish.</em></strong></p>\n<h4>Step6: Sending notification from your react native android application</h4>\n<ul>\n<li>First In order to send notification from your react native we need to generate fcm token</li>\n</ul>\n<pre><code class=\"language-js\">import messaging from \"@react-native-firebase/messaging\";\n\nconst App = () => {\n  const getFCMToken = async () => {\n    try {\n      const token = await messaging().getToken();\n      console.log(token);\n    } catch (e) {\n      console.log(error);\n    }\n  };\n\n  useEffect(() => {\n    getFCMToken();\n  }, []);\n\n  useEffect(() => {\n    //To handle background messaging\n    messaging().setBackgroundMessageHandler(async (remoteMessage) => {\n      let message_body = remoteMessage.notification.body;\n      let message_title = remoteMessage.notification.title;\n      let avatar = remoteMessage.notification.android.imageUrl;\n      Alert.alert(message_title, message_body);\n    });\n\n    //To handle foreground messaging\n    const subscribe = messaging().onMessage(async (remoteMessage) => {\n      let message_body = remoteMessage.notification.body;\n      let message_title = remoteMessage.notification.title;\n      let avatar = remoteMessage.notification.android.imageUrl;\n      Alert.alert(message_title, message_body);\n    });\n    return subscribe;\n  }, []);\n};\n</code></pre>","fields":{"slug":"/push-notification-in-react-native-with-firebase/"},"frontmatter":{"title":"Firebase push notification in react native android application","date":"December 25, 2022","description":"In this article i will explain you can implement firebase push notification in react native, android application","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/firebase-push.jpeg"}},"previous":{"fields":{"slug":"/package-nodejs-application-using-webpack/"},"frontmatter":{"title":"How to package nodejs application using webpack","date":"December 15, 2022","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/nodejs-webpack.jpeg"}},"next":{"fields":{"slug":"/python-opencv-tutorial-croping-resizing-facedetection-captureimagefromvideo/"},"frontmatter":{"title":"Python tutorial, reading and croping image, face detection, capturing image from video","date":"December 31, 2022","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/python.jpg"}}},"pageContext":{"id":"52705409-b4e8-54e0-8eff-9a29593793f8","previousPostId":"7702ffd3-33a4-5338-b1b1-356a66dc22c8","nextPostId":"2196f990-5637-5dd3-9a77-6eae13f365a9"}},
    "staticQueryHashes": ["3860684146"]}