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

Crashing when app is closed, but with different error #51

Open
skleest opened this issue Apr 4, 2016 · 16 comments
Open

Crashing when app is closed, but with different error #51

skleest opened this issue Apr 4, 2016 · 16 comments

Comments

@skleest
Copy link

skleest commented Apr 4, 2016

My app is also crashing @ RN 0.22.0 when app is killed, but I'm getting a different error:
I/ReactNativeJS(12477): 'GcmAndroid.launchNotification internal', undefined E/AndroidRuntime(12477): FATAL EXCEPTION: main E/AndroidRuntime(12477): Process: com.wayfarer, PID: 12477 E/AndroidRuntime(12477): java.lang.NoSuchMethodError: No virtual method onPause()V in class Lcom/facebook/react/ReactInstanceManager; or its super classes (declaration of 'com.facebook.react.ReactInstanceManager' appears in /data/app/com.wayfarer-2/base.apk) E/AndroidRuntime(12477): at com.oney.gcm.BackgroundService.onDestroy(BackgroundService.java:47) E/AndroidRuntime(12477): at android.app.ActivityThread.handleStopService(ActivityThread.java:3292) E/AndroidRuntime(12477): at android.app.ActivityThread.access$2300(ActivityThread.java:172) E/AndroidRuntime(12477): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1524) E/AndroidRuntime(12477): at android.os.Handler.dispatchMessage(Handler.java:102) E/AndroidRuntime(12477): at android.os.Looper.loop(Looper.java:145) E/AndroidRuntime(12477): at android.app.ActivityThread.main(ActivityThread.java:5835) E/AndroidRuntime(12477): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(12477): at java.lang.reflect.Method.invoke(Method.java:372) E/AndroidRuntime(12477): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) E/AndroidRuntime(12477): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Any suggestions here? Thank you.

@gijoehosaphat
Copy link

I get this same error. The notification does appear, and can be clicked on. But the app crashes in the background. The result of this is that you cannot receive a follow up notification until the user launches/closes the app again.

[email protected]
[email protected]
[email protected]

@udfalkso
Copy link

I didn't get this error, but I believe this is probably the same issue as the others are having. The way this lib is written, it's trying to instantiate the entire react app when receiving a background notification and some of the native modules (in my case FBSDK caused most of the issues) just won't instantiate nicely in this scenario for some reason.

For most use cases, this is probably overkill. I ended up writing a very minimal background service that just shows the text of the notification via the react-native-system-notification lib and nothing more. This workaround may help you as well.

First remove com.oney.gcm from this line in your Manifest:

<service android:name=".BackgroundService"></service>

Now add BackgroundService.java to your project:

package com.myapp;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import io.neson.react.notification.NotificationAttributes;
import android.os.Bundle;
import io.neson.react.notification.Notification;

public class BackgroundService extends Service {
    private static final String TAG = "BackgroundService";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand");

        Bundle bundle = intent.getBundleExtra("bundle");
        String body = bundle.getString("gcm.notification.body", "You have a notification");
        String subject = bundle.getString("gcm.notification.subject", "MyApp Title");

        NotificationAttributes attributes = new NotificationAttributes();
        attributes.delayed = false;
        attributes.scheduled = false;
        attributes.autoClear = true;
        attributes.inboxStyle = false;
        attributes.priority = 2;
        attributes.sound = "default";
        attributes.smallIcon = "ic_launcher";
        attributes.message = body;
        attributes.subject = subject;
        Notification notification = new Notification(this, 1, attributes);
        notification.create();

        return START_NOT_STICKY;
    }

}

@gijoehosaphat
Copy link

gijoehosaphat commented Apr 22, 2016

Hmm, I think these may be different issues though. I have gotten other errors similar to #54.

I had to rearrange my client entry js to ensure none of my App would be imported or registered if we were receiving a notification. Like this:

'use strict'

import React from 'react-native'
import GcmAndroid from 'react-native-gcm-android'
import Notification from 'react-native-system-notification'

if (GcmAndroid.launchNotification) {
  Notification.create({
    subject: GcmAndroid.launchNotification.title,
    message: GcmAndroid.launchNotification.body,
  })
  GcmAndroid.stopService()
} else {
  //We don't want the app loading up as normal when the app receives
  //a notification. So we only load this when the app launches as normal.
  const App = require('./containers/App').default
  React.AppRegistry.registerComponent('WhyWait', () => App)
}

@stinju
Copy link

stinju commented Apr 28, 2016

Re: onPause(), React Native changed some signatures in facebook/react-native@19a1c4c#diff-dc0f3a09c238b372cb1c27aa5f7dcbce. I pointed this plugin to RN 0.24.1 (the one I'm using), changed the calls to onPause() and onDestroy(), and things seem to be working so far - however, I haven't done a thorough inspection for any remaining dependencies on 0.17.+.

build.gradle:

// add below
allprojects {
    repositories {
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "../../../node_modules/react-native/android"
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.facebook.react:react-native:0.24.+'     // change from 0.17.+
    compile 'com.google.android.gms:play-services-gcm:8.1.0'
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile project(':react-native-system-notification')
}

BackgroundService.java:

        mReactInstanceManager.onHostPause(); // change from onPause
        mReactInstanceManager.destroy();     // change from onDestroy

@peterlazar1993
Copy link

@stinju Can you elaborate a little more, I tried changing those but my build failed with
/Users/pete/Code/Play/React-Native/Awesome/node_modules/react-native-gcm-android/android/src/main/java/com/oney/gcm/BackgroundService.java:47: error: cannot find symbol mReactInstanceManager.onHostPause(); ^ symbol: method onHostPause() location: variable mReactInstanceManager of type ReactInstanceManager /Users/pete/Code/Play/React-Native/Awesome/node_modules/react-native-gcm-android/android/src/main/java/com/oney/gcm/BackgroundService.java:48: error: cannot find symbol mReactInstanceManager.destroy(); ^ symbol: method destroy() location: variable mReactInstanceManager of type ReactInstanceManager 2 errors

@stinju
Copy link

stinju commented May 4, 2016

@peterlazar1993 were you able to resolve your issue? Looks like your build wasn't compiling against an RN version with the new signatures. Check to see if the 'onHostPause' method is implemented in ReactInstanceManager.java in your RN version. If so, then you might need to fiddle with the changes I mentioned above to get your build to use your RN version for this package. BTW, I wasn't clear above, but the build.gradle file mentioned is the one for react-native-gcm-android, not the one for your project/app.

@jojonarte
Copy link

Did you resolve this already? How did you solve this issue?

@peterlazar1993
Copy link

@jojonarte We decided to write a custom BackgroundService

@vmakhaev
Copy link

vmakhaev commented May 6, 2016

I resolved this error by creating a fork where removed not existing methods. And another thing is you should not require whole App while in background service, I'm not sure why, but maybe because some environment is not initialised.

@peterlazar1993
Copy link

@vmakhaev What kind of background service did you write? Care to share?

@vmakhaev
Copy link

vmakhaev commented May 6, 2016

@peterlazar1993 I used the one from react-native-gcm-android

@savioseb
Copy link

savioseb commented May 7, 2016

Comment the
GcmAndroid.stopService();

line.

It should work.

My index.android.js snippet:

`
if (GcmAndroid.launchNotification) {
console.log('Before entering function:', GcmAndroid.launchNotification);
//GcmAndroid.stopService();
console.log('GcmAndroid.launchNotification:', GcmAndroid.launchNotification);
var notification = GcmAndroid.launchNotification;
var info = JSON.parse(notification.info);
Notification.create({
subject: info.subject,
message: info.message,
});

//GcmAndroid.stopService();
} else {
class Savio extends Component {
`

@savioseb
Copy link

savioseb commented May 7, 2016

#22 also addressed by doing this change.

@samcarlosimpres
Copy link

@udfalkso Your solution works, but there's any way to make it show the message received by an external API instead of a static one??

@udfalkso
Copy link

@samcarlosimpres Yes. The code fetches the message from the notification, the static string there is just a fallback.

For me the value is coming through in the "gcm.notification.body" key of the bundle, perhaps for you it is at another key.

bundle.getString("gcm.notification.body", "You have a notification");

@openGeeksLab
Copy link

guys, sorry.
But how to receive notification when app is fully closed on Android?

wangdaliu added a commit to wangdaliu/react-native-gcm-android that referenced this issue Aug 25, 2016
wangdaliu added a commit to wangdaliu/react-native-gcm-android that referenced this issue Aug 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants