oney / react-native-gcm-android

GCM for React Native Android
MIT License
172 stars 76 forks source link

App crashes when closed while receiving notification #54

Open jlugner opened 8 years ago

jlugner commented 8 years ago

For some reason, my app crashes if I receive a push notification when the app is closed. It works just fine when the app is in foreground/background, but if I actually close it, notifications will cause a crash. My logs contain the following:

D/com.oney.gcm.GcmModule(20991): mIntent is null: false
D/com.oney.gcm.GcmModule(20991): bundleString: {"info":"{\"subject\":\"Reminder\",\"message\":\"Test\"}","collapse_key":"do_not_collapse"}
I/ReactNativeJS(20991): 'GcmAndroid.launchNotification internal', undefined
E/ReactNativeJS(20991): undefined is not an object (evaluating 's.getCurrentAppState')
E/AndroidRuntime(20991): FATAL EXCEPTION: mqt_native_modules
E/AndroidRuntime(20991): Process: com.myapp, PID: 20991
E/AndroidRuntime(20991): com.facebook.react.modules.core.JavascriptException: undefined is not an object (evaluating 's.getCurrentAppState'), stack:
E/AndroidRuntime(20991): <unknown>@12:23852
E/AndroidRuntime(20991): r@1:747
E/AndroidRuntime(20991): n@1:413
E/AndroidRuntime(20991): AppState@13:49968
E/AndroidRuntime(20991): <unknown>@24:12816
E/AndroidRuntime(20991): r@1:747
E/AndroidRuntime(20991): r@1:476
E/AndroidRuntime(20991): n@1:413
E/AndroidRuntime(20991): <unknown>@24:28048
E/AndroidRuntime(20991):  at com.facebook.react.modules.core.ExceptionsManagerModule.showOrThrowError(ExceptionsManagerModule.java:70)
E/AndroidRuntime(20991):  at com.facebook.react.modules.core.ExceptionsManagerModule.reportFatalException(ExceptionsManagerModule.java:58)
E/AndroidRuntime(20991):  at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(20991):  at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(20991):  at com.facebook.react.bridge.BaseJavaModule$JavaMethod.invoke(BaseJavaModule.java:249)
E/AndroidRuntime(20991):  at com.facebook.react.bridge.NativeModuleRegistry$ModuleDefinition.call(NativeModuleRegistry.java:158)
E/AndroidRuntime(20991):  at com.facebook.react.bridge.NativeModuleRegistry.call(NativeModuleRegistry.java:58)
E/AndroidRuntime(20991):  at com.facebook.react.bridge.CatalystInstanceImpl$NativeModulesReactCallback.call(CatalystInstanceImpl.java:428)
E/AndroidRuntime(20991):  at com.facebook.react.bridge.queue.NativeRunnableDeprecated.run(Native Method)
E/AndroidRuntime(20991):  at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime(20991):  at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(20991):  at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
E/AndroidRuntime(20991):  at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(20991):  at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:184)
E/AndroidRuntime(20991):  at java.lang.Thread.run(Thread.java:818)
jlugner commented 8 years ago

This only seems to happen when I import AppState from react-native in the same file as my notification-code. Any ideas here?

udfalkso commented 8 years ago

I just wrote about a possible workaround here: https://github.com/oney/react-native-gcm-android/issues/51#issuecomment-212933067

savioseb commented 8 years ago

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 {`

RonakKhandelwal commented 8 years ago

@savioseb It still crashes!! any possible workaround?

chansila commented 7 years ago

As @udfalkso mentioned I have rewrite his code for workaround to get notification dynamically from GCM itself.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Bundle bundle = intent.getBundleExtra("bundle");
    Object result = bundle.get("info");
    String data   = result.toString();
    String id     = bundle.get("google.message_id").toString();
    try{
        JSONObject jsonObj = new JSONObject(data);
        Log.d(TAG,"onStartCommand jsonObj => :"+jsonObj);
        Log.d(TAG,"onStartCommand bundle => :"+bundle);
        String message = jsonObj.getString("message");
        String subject = jsonObj.getString("subject");
        Integer notificationId = Integer.parseInt(id.substring(0, 10).replaceAll("[\\D]", ""));

        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 = message;
        attributes.subject = subject;
        Notification notification = new Notification(this, notificationId, attributes);
        notification.create();

    }catch (JSONException e) {
       Log.d(TAG,e);
    }  
    return START_NOT_STICKY;
}