React-native-fcm: Android crashes when receiving notification

Created on 28 Jul 2016  ยท  32Comments  ยท  Source: evollu/react-native-fcm

I'm running into the following issue when using the latest [email protected] and [email protected]

I have setup the package as outlined in the documentation with the only thing still not quite clear to me whether to add the public onNewIntent() method to MainActivity.java. i get the same crash whether i add it or not though so it does not seem to make a difference.

I thnk the docs are a bit unclear here since RN>0.29 apps now use MainApplication.java for additional activity methods as far as i can tell.

Either way, what happens is; while notifications are received fine on the device (tested on simulator) once a notification is received when the app is not in focus or started and you start the app it immediately crashes.

I get the following stack trace from Android Studio:

07-28 16:13:53.355 4733-4733/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.florinapp.florinapp.debug, PID: 4733
                                                 java.lang.AbstractMethodError: abstract method "void com.facebook.react.bridge.ActivityEventListener.onNewIntent(android.content.Intent)"
                                                     at com.facebook.react.bridge.ReactContext.onNewIntent(ReactContext.java:169)
                                                     at com.facebook.react.ReactInstanceManagerImpl.onNewIntent(ReactInstanceManagerImpl.java:483)
                                                     at com.facebook.react.ReactActivity.onNewIntent(ReactActivity.java:183)
                                                     at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1223)
                                                     at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1235)
                                                     at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2783)
                                                     at android.app.ActivityThread.performNewIntents(ActivityThread.java:2795)
                                                     at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2804)
                                                     at android.app.ActivityThread.-wrap15(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1539)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:154)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Most helpful comment

it is image picker

All 32 comments

Probably useful to know the preceding line (before it crashes) is:

07-28 16:39:49.742 5159-5198/? I/ReactNativeJS: JSONLOG: {"type":"log","messages":["push notification received",{"fcm":{"action":"android.intent.action.MAIN"},"profile":0}]}

are you adding public void newIntent(Intent intent) { in MainActivity.java? it is no longer needed in 1.0.16

Yes thats how i interpreted the docs. I tried it with as well since it crashed as described above. But the error is the same in both cases.

so it crashes without adding the override method at all? can you share an example repo?

having "almost" the same problem here on android

  • when app is in foreground it receives and handles notification
  • when app is closed and I tap on a notification, it opens the app but doesn't handle the notification
  • when the app is in background and I tap on a notification it will crash...

I thought maybe there's something wrong with my 'intents' or 'click_action', any thoughts?
I'm using react-native-fcm 1.0.16 and RN 0.30, I've tried both with and without public void onNewIntent (Intent intent) {} in MainActivity.java

UPDATE: figured out why it wasn't handling when app was closed, I wasn't using FCM.initialData it works now... still trying to figure out why it crashes when app is in background

let me try it out

I have this in my manifest:

      <activity
        android:name=".MainActivity"
        android:label="@string/PRODUCT_NAME"
        android:launchMode="singleTop"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="soheil.action.NOTIFICATION" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
      </activity>

and on the server, I'm using Node.js with node-gcm, sending this:

    var message = new gcm.Message({
      priority: 'high',
      contentAvailable: true,
      restrictedPackageName: 'com.jadidian.soheil.test',
      data: { foo: 'bar' },
      notification: {
        title: 'Hello!',
        icon: 'ic_launcher',
        body: 'Test message',
        click_action: 'soheil.action.NOTIFICATION'
      }
    });

still no luck...

I sort of know why. will fix tomorrow

nope. can't reproduce.
Are you using other library together with it?

on Android, no other messaging library, but I had to hack the build.gradle a bit, because react-native-maps uses com.google.android.gms:play-services:8.4.0

dependencies {
    compile project(':realm')
    compile project(':react-native-permissions')
    compile project(':react-native-material-kit')
    compile project(':react-native-image-picker')
    compile project(':react-native-fs')
    compile project(':react-native-fcm')
    compile project(':react-native-fbsdk')
    compile project(':react-native-device-info')
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.facebook.react:react-native:+' // From node_modules
    compile 'com.airbnb.android:react-native-maps:0.7.1'
    compile 'com.google.android.gms:play-services-maps:9.2.1' // Because react-native-maps causes multiple dex exception
    compile 'com.google.firebase:firebase-core:9.2.1'
    compile 'com.google.firebase:firebase-messaging:9.2.1'
}

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

can you share your code? or create a simple project?

the whole project is in a private repo on Bitbucket, I can either include you there (if you're on Bitbucket) or I can create a simple project and share it on Github, and thanks for your help

it is evollu there

I've sent the invitation, clone the repo, checkout 1.0.3, run npm install and then npm run android-development hopefully it will work...

Btw, if you had any questions etc. https://chat.stackoverflow.com/rooms/118588/raly

you have react-native-push-notification and they had an issue https://github.com/zo0r/react-native-push-notification/issues/150. Is that causing issue?

yes but I'm only using it on iOS

it is linked in android project

did you run react-native link? it shouldn't be linked otherwise... btw, I think the stackoverflow chatroom is a better place to discuss

i can't post in that chat room because I dont have 20 points. can you use gitter?

I didn't link anything, just opened the gradle using android studio

on releases/1.0.3 branch? (just signed up in gitter but really don't know how to use it)

go to code tab and there is a green button

@tijs this issue happen when some library adds listener to reactContext.addActivityEventListener(this); while not implementing overriding function for onNewIntent.
the build succeed because in gradle it depends on lower react native version. search for addActivityEventListener and see what library has it but doesn't implement onNewIntent

@evollu yeah i was thinking along those lines but no other library i use implements such a listener. The closest i get is react-native-share which opens links using a web intent like so:

this.setIntent(new Intent(new Intent("android.intent.action.VIEW", Uri.parse(url))));

But i don't see how that could be causing issues.

These are my dependencies:

dependencies {
    compile project(':react-native-segment-io-analytics')
    compile project(':react-native-code-push')
    compile project(':react-native-vector-icons')
    compile project(':react-native-contacts')
    compile project(':react-native-fcm')
    compile project(':react-native-share')
    compile project(':react-native-material-kit')
    compile project(':react-native-image-picker')
    compile project(':react-native-device-info')
    compile 'net.hockeyapp.android:HockeySDK:4.0.1'
    compile 'com.google.firebase:firebase-messaging:9.0.0'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.facebook.react:react-native:+'
}

And my manifest looks like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.florinapp">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.camera.any" android:required="true" />
    <uses-feature android:name="android.hardware.screen.portrait" android:required="true" />

    <application
        android:name=".MainApplication"
        android:allowBackup="true"
        android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:launchMode="singleTop"
            android:alwaysRetainTaskState="true">

            <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter android:autoVerify="true">
              <action android:name="android.intent.action.VIEW" />
              <category android:name="android.intent.category.DEFAULT" />
              <category android:name="android.intent.category.BROWSABLE" />
              <!-- Accepts URIs that begin with "florinapp://โ€ -->
              <data android:scheme="florinapp"
                  android:host="claim"/>
              <data android:scheme="florinapp"
                  android:host="iban-gekoppeld"/>
                <data android:scheme="florinapp"
                    android:host="payment-done"/>
            </intent-filter>

            <intent-filter>
                <action android:name="fcm.ACTION.HELLO" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>

        <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />

        <service android:name="com.evollu.react.fcm.MessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

        <service android:name="com.evollu.react.fcm.InstanceIdService" android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

        <meta-data android:name="net.hockeyapp.android.appIdentifier" android:value="${HOCKEYAPP_APP_ID}" />

    </application>
</manifest>

Could it be an issue that the DEFAULT intent category is set for deep links too? Seems like that should not matter but perhaps that conflicts if fcm is expecting a different type of intent on startup?

If i comment out the DEFAULT category for the URI intents it still crashes after having received a background notification:

 <intent-filter android:autoVerify="true">
     <!--<category android:name="android.intent.category.DEFAULT" />-->
     ...

So that's also not it. Like @jadidian my repo is private but i can try and setup a test app if that will help. Would take a bit of time though since i would need to setup firebase keys, etc to test properly.

it is image picker

haven't tried yet but I think they've fixed the issue https://github.com/marcshilling/react-native-image-picker/issues/275

Thats awesome! Thanks guys. It's night here again but I'll try that tomorrow. @evollu how did you figure out it was image picker? I tried searching for anything implementing activitylistener but that did not turn up anything useful.

it came out in my search.

yup that was it. thanks again!

Was this page helpful?
0 / 5 - 0 ratings