How to use Firebase Notification in an React-Native project

Asked

Viewed 2,425 times

1

To Create a React-Native project with Firebase notifications

Steps to add pushNotification

Link: https://rnfirebase.io/docs/v4.0.x/installation/initial-setup

  • Create or open an React Native project (test before.)
  • Assign all permissions to that directory
  • Add pakege 'React-Native-firebase':

npm install --save React-Native-firebase

React-Native link React-Native-firebase

Procedure on Android OS

Add file 'google-services.json'

Update code on: android/Gradle/wrapper/Gradle-wrapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

Add code in android/setings.Radle

include ':react-native-firebase'
project(':react-native-firebase').projectDir = new 
File(rootProject.projectDir, '../node_modules/react-native-firebase/android')

Adding codes to the /android/build.Gradle file

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0'
        classpath 'com.google.gms:google-services:3.2.1'
    }
}
allprojects {
    repositories {
        mavenLocal()
        jcenter()
        google()
        maven {
            //...
            url "$rootDir/../node_modules/react-native/android"
        }
    }
}

Add code to file /android/app/build.Radle

android {
    compileSdkVersion 27
    buildToolsVersion "23.0.1"

    defaultConfig {
        //...
        minSdkVersion 23
        targetSdkVersion 27
        //...
    }
    //...
}
dependencies {
    //Substituir 'compile' por 'implementation' em todas as dependencies
    implementation fileTree(dir: "libs", include: ["*.jar"])

    //atualizar appcompat para 'appcompat-v7:27.0.+'
    implementation "com.android.support:appcompat-v7:27.0.+"

    implementation "com.facebook.react:react-native:+"  
    implementation(project(':react-native-firebase')) {
        transitive = false
    }
    // Firebase dependencies
    implementation "com.google.firebase:firebase-core:15.0.2"
    implementation "com.google.firebase:firebase-messaging:15.0.2"
}
//Add 'google()' aos dois repositórios
repositories {
    google()
}
buildscript {
    repositories {
        google()
    }
}
//add linha ao final do documento
apply plugin: 'com.google.gms.google-services'

Edit React-Native-mauron85-background-Geolocation

Edit code in node_modules/React-Native-mauron85-background-Geolocation/android/lib/build.Gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 26
    buildToolsVersion "23.0.3"

    defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 8
        versionName "0.5.0-alpha.3"
    }
}

dependencies {
    //...
    compile 'com.android.support:support-v4:26.+'
    //...
}

Add line in android/Gradle.properties

android.enableAapt2=false

Add new string line to: /android/app/src/main/res/values/string.xml

 <string name="default_notification_channel_id">allDevicesApp</string>

Add code to file /android/app/src/main/Androidmanifest.xml

<manifest ...>
  <!-- Add permissões -->
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
  <uses-permission android:name="android.permission.VIBRATE"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <!-- Definir constante SDK para referência -->
  <uses-sdk
      android:minSdkVersion="23"
      android:targetSdkVersion="27" />
  <application>
  <!--Definir icone padrão para notificação -->
    <meta-data
      android:name="com.google.firebase.messaging.default_notification_icon"
      android:resource="@mipmap/ic_launcher" />
    <!--Definir canal padrão de push para todos os usuários-->
    <meta-data
      android:name="com.google.firebase.messaging.default_notification_channel_id"
      android:value="@string/default_notification_channel_id"/>
      <!-- Serviço para Push Notification -->
      <service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
        <intent-filter>
          <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
      </service>
      <service android:name="io.invertase.firebase.messaging.RNFirebaseInstanceIdService">
        <intent-filter>
          <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
      </service>
      <receiver android:name="io.invertase.firebase.notifications.RNFirebaseNotificationReceiver"/>
      <receiver android:enabled="true" android:exported="true"  android:name="io.invertase.firebase.notifications.RNFirebaseNotificationsRebootReceiver">
        <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED"/>
          <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
          <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
          <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
      </receiver>
  </application>
</manifest>

In: android/app/src/main/java/{package}/Mainaplication.java

-Add libraries in:-

import io.invertase.firebase.messaging.RNFirebaseMessagingPackage; 
import io.invertase.firebase.notifications.RNFirebaseNotificationsPackage;

-Add constructors to the method:

protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
      //...
      new RNFirebaseMessagingPackage(),
      new RNFirebaseNotificationsPackage()
  );
}
  • Ready!
  • Finally, just use the Rnfirebase.io documentation and be happy
  • Rnfirebase.io
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.