evollu / react-native-fcm

react native module for firebase cloud messaging and local notification
MIT License
1.73k stars 681 forks source link

App crashes when receiving push notification in Android foreground only #756

Open lfaz opened 6 years ago

lfaz commented 6 years ago

Tried multiple devices same it crashes, I am using react native 0.49, can receive notifications on background, but not when app is open! I am using "react-native-fcm": "^11.3.1" and using google services 11.8.0 on my app bundle!

MelissaWeng commented 6 years ago

We are in the same project with this error:

FATAL EXCEPTION: main
                                                                       Process:xx.xxxx.xxxx, PID: 24776
                                                                       java.lang.ClassCastException: xx.xxxx.xxxx.MainApplication cannot be cast to com.facebook.react.ReactApplication
                                                                           at com.evollu.react.fcm.MessagingService$1.run(MessagingService.java:41)
                                                                           at android.os.Handler.handleCallback(Handler.java:751)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6682)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
evollu commented 6 years ago

@MelissaWeng you are using exponent? Haven't figured out a way to coexist with exponent yet

evollu commented 6 years ago

@lulzimfazlija try leave your notification callback empty and see if it crashes

MelissaWeng commented 6 years ago

@evollu the app is detached from Expo and just only use ExpoKit to publish the app. And push notification works in the background, just crash the app when app is open and receiving the notification.

evollu commented 6 years ago

@MelissaWeng in that case check if logic in notification callback causes crash

MelissaWeng commented 6 years ago

@evollu Notification callback is left empty, but still app crashes in foreground (and works in background).

evollu commented 6 years ago

weird. can you provide an example repo? does the example project crash?

lfaz commented 6 years ago

@evollu may I ask if you ever been able to make it work for android for both background and foreground? If so, can you prove some code of the MessageService.java also the MainApplication.java

evollu commented 6 years ago

@lulzimfazlija there is an example project in the repo

MelissaWeng commented 6 years ago

Hi, I just checked that my app targetSdkVersion = 26, and do you think it is the reason causing the crash? And how shall I proceed with it?

evollu commented 6 years ago

react-native itself targets 23. so to be safe use targetSdkVersion = 23

lfaz commented 6 years ago

@evollu what do you think it is causing the crash on foreground?

Mainapplication.java looks as below:

// Needed for `react-native link`
  public List<ReactPackage> getPackages() {
    return Arrays.<ReactPackage>asList(
        // Add your own packages here!
        // TODO: add cool native modules
//            new MainReactPackage(),

            new RNBackgroundGeolocation(),
            // Needed for `react-native link`

            new FIRMessagingPackage(),
            //new RNBackgroundGeolocation(),
            //new RNFirebasePackage(),
            new VectorIconsPackage()
            //new RNFirebaseMessagingPackage()
    );
  }

and notification handler, listener looks like:

`this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => { console.warn("listening.......................");

        if (notif.fcm) {
            console.log("sending notification...");
            this.title = notif.fcm.title
            this.body = notif.fcm.body

            FCM.presentLocalNotification({
                title: notif.title,                     // as FCM payload
                body: notif.body,                    // as FCM payload (required)
                priority: "high",                                   // as FCM payload
                click_action: "ACTION",                             // as FCM payload
                auto_cancel: true,                                  // Android only (default true)
                vibrate: 300,                                       // Android only default: 300, no vibration if you pass null
                lights: true,                                       // Android only, LED blinking (default false)
                show_in_foreground: true,                           // show notification when app is in foreground (local & remote)
                local: true
            })
        }

        // optional, do some component related stuff
    });`

ANy idea what would it cause the crash on foreground?

AugustoAleGon commented 6 years ago

I still have the same issue!

lfaz commented 6 years ago

i gave up using this library as it is enough crappy to waste your time, go and use onesignal, it is quite easy to setup and works like a charm for both platforms!

nirpeled commented 6 years ago

@lulzimfazlija @MelissaWeng Did you manage to get this working with ExpoKit? Yeah - OneSignal works great and it is easy to integrate but you "pay" in privacy... I managed to get the build work just fine but the app crashes on startup 😞

ohsory1324 commented 6 years ago

@nirpeled Did you check #813 ?