Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onMessage not being triggered #1173

Closed
haseebnaseem opened this issue Jun 4, 2018 · 6 comments
Closed

onMessage not being triggered #1173

haseebnaseem opened this issue Jun 4, 2018 · 6 comments
Labels
plugin: messaging FCM only - ( messaging() ) - do not use for Notifications

Comments

@haseebnaseem
Copy link

Issue

onMessage event is not triggered. My index.js file:

import React,{Component} from 'react';
import EStyleSheet from 'react-native-extended-stylesheet';
import store from './config/store';
import {Provider} from 'react-redux';
import AppNavigationState from './config/routes';
import {Alert} from './components/alert';
import codePush from 'react-native-code-push';
import firebase from 'react-native-firebase'
EStyleSheet.build({
      $primaryRed: '#C21F2B',
});

class App extends Component{
    componentDidMount(){
        console.log('here right now');
        this.messageListener = firebase.messaging().onMessage(message=>{
            console.log('here');
            console.log(message);
        });
    }
    componentWillUnmount(){
        this.messageListener();
    }
    render(){
        return (
            <Provider store={store}>
                <Alert>
                    <AppNavigationState/>
                </Alert>
            </Provider>
        );
    }
}
export default codePush({updateDialog:true,installMode:codePush.InstallMode.IMMEDIATE})(App);

android/app/build.gradle

apply plugin: "com.android.application"

import com.android.build.OutputFile

/**
 * The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
 * and bundleReleaseJsAndAssets).
 * These basically call `react-native bundle` with the correct arguments during the Android build
 * cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the
 * bundle directly from the development server. Below you can see all the possible configurations
 * and their defaults. If you decide to add a configuration block, make sure to add it before the
 * `apply from: "../../node_modules/react-native/react.gradle"` line.
 *
 * project.ext.react = [
 *   // the name of the generated asset file containing your JS bundle
 *   bundleAssetName: "index.android.bundle",
 *
 *   // the entry file for bundle generation
 *   entryFile: "index.android.js",
 *
 *   // whether to bundle JS and assets in debug mode
 *   bundleInDebug: false,
 *
 *   // whether to bundle JS and assets in release mode
 *   bundleInRelease: true,
 *
 *   // whether to bundle JS and assets in another build variant (if configured).
 *   // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
 *   // The configuration property can be in the following formats
 *   //         'bundleIn${productFlavor}${buildType}'
 *   //         'bundleIn${buildType}'
 *   // bundleInFreeDebug: true,
 *   // bundleInPaidRelease: true,
 *   // bundleInBeta: true,
 *
 *   // whether to disable dev mode in custom build variants (by default only disabled in release)
 *   // for example: to disable dev mode in the staging build type (if configured)
 *   devDisabledInStaging: true,
 *   // The configuration property can be in the following formats
 *   //         'devDisabledIn${productFlavor}${buildType}'
 *   //         'devDisabledIn${buildType}'
 *
 *   // the root of your project, i.e. where "package.json" lives
 *   root: "../../",
 *
 *   // where to put the JS bundle asset in debug mode
 *   jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
 *
 *   // where to put the JS bundle asset in release mode
 *   jsBundleDirRelease: "$buildDir/intermediates/assets/release",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in debug mode
 *   resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in release mode
 *   resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
 *
 *   // by default the gradle tasks are skipped if none of the JS files or assets change; this means
 *   // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
 *   // date; if you have any other folders that you want to ignore for performance reasons (gradle
 *   // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
 *   // for example, you might want to remove it from here.
 *   inputExcludes: ["android/**", "ios/**"],
 *
 *   // override which node gets called and with what additional arguments
 *   nodeExecutableAndArgs: ["node"],
 *
 *   // supply additional arguments to the packager
 *   extraPackagerArgs: []
 * ]
 */

project.ext.react = [
    entryFile: "index.js"
]

apply from: "../../node_modules/react-native/react.gradle"
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"

/**
 * Set this to true to create two separate APKs instead of one:
 *   - An APK that only works on ARM devices
 *   - An APK that only works on x86 devices
 * The advantage is the size of the APK is reduced by about 4MB.
 * Upload all the APKs to the Play Store and people will download
 * the correct one based on the CPU architecture of their device.
 */
def enableSeparateBuildPerCPUArchitecture = true

/**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
def enableProguardInReleaseBuilds = true

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    dexOptions {
        jumboMode true
    }
    defaultConfig {
        applicationId "com.paitoo"
        minSdkVersion 16
        targetSdkVersion 22
        vectorDrawables.useSupportLibrary=true
        versionCode 17
        versionName "0.0.7"
        multiDexEnabled true
    }
    signingConfigs {
        release {
            if(project.hasProperty('MYAPP_RELEASE_STORE_FILE')){
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            }
        }
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    buildTypes {
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
        }
    }
    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a":1, "x86":2]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
}

dependencies {
    implementation project(':react-native-firebase')
    implementation project(':react-native-gesture-handler')
    implementation project(':react-native-linear-gradient')
    implementation project(':react-native-spinkit')
    implementation project(':react-native-fetch-blob')
    implementation project(':react-native-image-crop-picker')
    implementation project(':appcenter-crashes')
    implementation project(':appcenter-analytics')
    implementation project(':appcenter')
    implementation project(':react-native-code-push')
    implementation project(':react-native-fbsdk')
    implementation project(':react-native-vector-icons')
    implementation project(':react-native-google-signin')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:23.0.1"
    implementation "com.facebook.react:react-native:+"  // From node_modules
    implementation "com.google.android.gms:play-services-base:15.0.0"
    implementation "com.google.android.gms:play-services-auth:15.0.0"
    implementation "com.google.firebase:firebase-core:15.0.2"
    implementation "com.google.firebase:firebase-messaging:15.0.2"
    implementation 'me.leolin:ShortcutBadger:1.1.21@aar'
    implementation "com.android.support:multidex:1.0.2"
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply plugin: 'com.google.gms.google-services'

logs from adb logcat

06-04 18:03:16.715 12354 12482 D RNFMessagingService: onMessageReceived event received
06-04 18:03:16.717 12354 12354 D RNFirebaseMessaging: Received new message
 06-04 18:03:18.813 12354 12484 D RNFMessagingService: onMessageReceived event received
 06-04 18:03:18.819 12354 12354 D RNFirebaseMessaging: Received new message
06-04 18:03:19.919 12354 12487 D RNFMessagingService: onMessageReceived event received
06-04 18:03:19.927 12354 12354 D RNFirebaseMessaging: Received new message

sending post request using postman, The request is as follows:

{
  "to" : "<reg-id>",
  "priority" : "high",
  "data":{
  	"name":"some name"
  }
}

response from the request

{
    "multicast_id": 6238365495799350302,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "0:1528117400683408%42f8cac9f9fd7ecd"
        }
    ]
}

Environment

  1. Application Target Platform: android
  1. Development Operating System: Ubuntu(16.04)
  1. Build Tools:
  1. React Native version:0.48
  1. RNFirebase Version:4.2.2
  1. Firebase Module:15.0.0
@Ehesp Ehesp added the plugin: messaging FCM only - ( messaging() ) - do not use for Notifications label Jun 8, 2018
@mattryles
Copy link

can you share the fix?

1 similar comment
@amirasaran
Copy link

can you share the fix?

@ShooterArk
Copy link

Did anyone solve this issue :/ ..

@quachsimon
Copy link

Also having an issue with this... it seems only onNotification is being called. I am following the docs at https://rnfirebase.io/docs/v4.3.x/messaging/receiving-messages but, none of it seems to be working when I try to trigger a message from the Firebase console

@mattryles
Copy link

Hey,
You need to add both the messaging and notifications libraries, then set the event handler to something like:
firebase.notifications().onNotification((noti: Notification) => {
firebase.notifications().displayNotification(noti);
});
Notifications are what happens when the app is in the foreground and messages are what happens the the app is in the background.

@dongcp
Copy link

dongcp commented Oct 2, 2018

Hey,
You need to add both the messaging and notifications libraries, then set the event handler to something like:
firebase.notifications().onNotification((noti: Notification) => {
firebase.notifications().displayNotification(noti);
});
Notifications are what happens when the app is in the foreground and messages are what happens the the app is in the background.

You have saved my day. Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: messaging FCM only - ( messaging() ) - do not use for Notifications
Projects
None yet
Development

No branches or pull requests

7 participants