react-native-gcm-android
react-native-gcm-android copied to clipboard
Crashing when app is closed, but with different error
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.
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.
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;
}
}
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)
}
Re: onPause()
, React Native changed some signatures in https://github.com/facebook/react-native/commit/19a1c4c22985ea6c7b475db71681bfc4dac4f1e0#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
@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
@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.
Did you resolve this already? How did you solve this issue?
@jojonarte We decided to write a custom BackgroundService
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.
@vmakhaev What kind of background service did you write? Care to share?
@peterlazar1993 I used the one from react-native-gcm-android
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 { `
#22 also addressed by doing this change.
@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??
@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");
guys, sorry. But how to receive notification when app is fully closed on Android?