1

Para Criar um projeto react-native com notificações do Firebase

Passos para adicionar pushNotification

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

  • Criar ou abrir um projeto do React Native (teste antes.)
  • Atribuir todas as permissões a esse diretório
  • Adicionar pakege 'react-native-firebase':

npm install --save react-native-firebase

react-native link react-native-firebase

Procedimento no Android OS

Adicionar arquivo 'google-services.json'

Atualizar código em: android/gradle/wrapper/gradle-wrapper.properties

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

Adicionar código em android/setings.gradle

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

Adicinar códigos ao arquivo /android/build.gradle

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"
        }
    }
}

Adicinar códigos ao arquivo /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'

Editar react-native-mauron85-background-geolocation

Editar código em 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.+'
    //...
}

Adicionar linha em android/gradle.properties

android.enableAapt2=false

Adicionar nova linha de string ao: /android/app/src/main/res/values/string.xml

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

Adicionar códigos ao arquivo /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>

Em: android/app/src/main/java/{pacote}/MainAplication.java

-Adicionar bibliotecas em:-

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

-Adicionar construtores no método:

protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
      //...
      new RNFirebaseMessagingPackage(),
      new RNFirebaseNotificationsPackage()
  );
}
  • Pronto!
  • Por fim, basta usar a documentação do RNFirebase.io e ser feliz
  • RNFirebase.io

0

Você deve fazer log-in para responder a esta pergunta.

Pesquise outras perguntas com a tag .