How to use Firebase Notification in a React-Native project

1

To Create a react-native project with Firebase notifications

Steps to add pushNotification

Link: link

  • Create or open a React Native project (test before.)
  • Assign all permissions to this 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'

  • Create the project on Google Cloud: link
  • Link project to Firebase: link
  • Add Android App to the Firebase project and download the 'google-services.json' file.
  • Move document 'google-services.json' to: /android/app/google-services.json

Update code in: android / gradle / wrapper / gradle-wrapper.properties

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

Add code in android / setings.gradle

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

Adding codes to /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"
        }
    }
}

Adding codes to /android/app/build.gradle

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 to: /android/app/src/main/res/values/string.xml

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

Adding codes to the 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;

-Adding constructors in 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
asked by anonymous 08.05.2018 / 15:48

0 answers