question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot use Firebase Background Data Messages feature

See original GitHub issue

Description

I’m using react-native-firebase to send notifications. But when I send data notification, when the app is in background/killed, it crashes the app.

Current behavior

It crashes the application.

Expected Behavior

It should start the app and perform the logic written in the HeadlessJs Task defined in the index.native.js file.

Possible Solution

Implement com.facebook.react.ReactApplication class for this to work

Steps to reproduce

  1. Add firebase messaging in your app.
  2. Register Firebase Token and copy it to the clipboard.
  3. Send data message(not notification) payload through the admin server using FCM API.
  4. Then you’ll be able to see the app crashing.

Android Logcat

02-23 16:34:59.943  3697  3697 E AndroidRuntime: FATAL EXCEPTION: main
02-23 16:34:59.943  3697  3697 E AndroidRuntime: Process: mobi.infolife.appbackup, PID: 3697
02-23 16:34:59.943  3697  3697 E AndroidRuntime: java.lang.RuntimeException: Unable to start receiver com.trustlook.sdk.receiver.PkgChangeReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { act=android.intent.action.PACKAGE_FULLY_REMOVED dat=package:com.alkymia.pop flg=0x5000010 cmp=mobi.infolife.appbackup/com.trustlook.sdk.service.ServicePkgChange (has extras) }: app is in background uid UidRecord{39106ce u0a425 RCVR idle change:uncached procs:1 seq(0,0,0)}
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	at android.app.ActivityThread.handleReceiver(ActivityThread.java:3389)
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	at android.app.ActivityThread.access$1200(ActivityThread.java:200)
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1662)
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:106)
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:193)
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:6692)
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
02-23 16:34:59.943  3697  3697 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { act=android.intent.action.PACKAGE_FULLY_REMOVED dat=package:com.alkymia.pop flg=0x5000010 cmp=mobi.infolife.appbackup/com.trustlook.sdk.service.ServicePkgChange (has extras) }: app is in background uid UidRecord{39106ce u0a425 RCVR idle change:uncached procs:1 seq(0,0,0)}
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1577)
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	at android.app.ContextImpl.startService(ContextImpl.java:1532)
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	at android.content.ContextWrapper.startService(ContextWrapper.java:664)
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	at android.content.ContextWrapper.startService(ContextWrapper.java:664)
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	at com.trustlook.sdk.receiver.PkgChangeReceiver.onReceive(PkgChangeReceiver.java:3)
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	at android.app.ActivityThread.handleReceiver(ActivityThread.java:3380)
02-23 16:34:59.943  3697  3697 E AndroidRuntime: 	... 8 more
--------- beginning of main
02-23 18:11:23.977 31745 31745 D AndroidRuntime: Shutting down VM
02-23 18:11:23.980 31745 31745 E AndroidRuntime: FATAL EXCEPTION: main
02-23 18:11:23.980 31745 31745 E AndroidRuntime: Process: sg.learncool.app, PID: 31745
02-23 18:11:23.980 31745 31745 E AndroidRuntime: java.lang.RuntimeException: Unable to start service io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService@f86051e with Intent { cmp=sg.learncool.app/io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService (has extras) }: java.lang.ClassCastException: sg.learncool.app.MainApplication cannot be cast to com.facebook.react.ReactApplication
02-23 18:11:23.980 31745 31745 E AndroidRuntime: 	at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3687)
02-23 18:11:23.980 31745 31745 E AndroidRuntime: 	at android.app.ActivityThread.access$1600(ActivityThread.java:200)
02-23 18:11:23.980 31745 31745 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1682)
02-23 18:11:23.980 31745 31745 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:106)
02-23 18:11:23.980 31745 31745 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:193)
02-23 18:11:23.980 31745 31745 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:6692)
02-23 18:11:23.980 31745 31745 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
02-23 18:11:23.980 31745 31745 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
02-23 18:11:23.980 31745 31745 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
02-23 18:11:23.980 31745 31745 E AndroidRuntime: Caused by: java.lang.ClassCastException: sg.learncool.app.MainApplication cannot be cast to com.facebook.react.ReactApplication
02-23 18:11:23.980 31745 31745 E AndroidRuntime: 	at com.facebook.react.HeadlessJsTaskService.getReactNativeHost(HeadlessJsTaskService.java:166)
02-23 18:11:23.980 31745 31745 E AndroidRuntime: 	at com.facebook.react.HeadlessJsTaskService.startTask(HeadlessJsTaskService.java:97)
02-23 18:11:23.980 31745 31745 E AndroidRuntime: 	at com.facebook.react.HeadlessJsTaskService.onStartCommand(HeadlessJsTaskService.java:48)
02-23 18:11:23.980 31745 31745 E AndroidRuntime: 	at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3668)
02-23 18:11:23.980 31745 31745 E AndroidRuntime: 	... 8 more
--------- beginning of system
02-23 18:11:29.186  1947  1947 D AndroidRuntime: Shutting down VM
02-23 18:11:29.192  1947  1947 E AndroidRuntime: FATAL EXCEPTION: main
02-23 18:11:29.192  1947  1947 E AndroidRuntime: Process: sg.learncool.app, PID: 1947
02-23 18:11:29.192  1947  1947 E AndroidRuntime: java.lang.RuntimeException: Unable to start service io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService@2cae8bd with Intent { cmp=sg.learncool.app/io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService (has extras) }: java.lang.ClassCastException: sg.learncool.app.MainApplication cannot be cast to com.facebook.react.ReactApplication
02-23 18:11:29.192  1947  1947 E AndroidRuntime: 	at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3687)
02-23 18:11:29.192  1947  1947 E AndroidRuntime: 	at android.app.ActivityThread.access$1600(ActivityThread.java:200)
02-23 18:11:29.192  1947  1947 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1682)
02-23 18:11:29.192  1947  1947 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:106)
02-23 18:11:29.192  1947  1947 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:193)
02-23 18:11:29.192  1947  1947 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:6692)
02-23 18:11:29.192  1947  1947 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
02-23 18:11:29.192  1947  1947 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
02-23 18:11:29.192  1947  1947 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
02-23 18:11:29.192  1947  1947 E AndroidRuntime: Caused by: java.lang.ClassCastException: sg.learncool.app.MainApplication cannot be cast to com.facebook.react.ReactApplication
02-23 18:11:29.192  1947  1947 E AndroidRuntime: 	at com.facebook.react.HeadlessJsTaskService.getReactNativeHost(HeadlessJsTaskService.java:166)
02-23 18:11:29.192  1947  1947 E AndroidRuntime: 	at com.facebook.react.HeadlessJsTaskService.startTask(HeadlessJsTaskService.java:97)
02-23 18:11:29.192  1947  1947 E AndroidRuntime: 	at com.facebook.react.HeadlessJsTaskService.onStartCommand(HeadlessJsTaskService.java:48)
02-23 18:11:29.192  1947  1947 E AndroidRuntime: 	at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3668)
02-23 18:11:29.192  1947  1947 E AndroidRuntime: 	... 8 more

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
saghulcommented, Feb 25, 2020

MainApplication does not inherit from a ReactApplication

0reactions
kartalsezcommented, Feb 24, 2022

I can’t reach @hmharshit, he wrote that he solved this issue via inheriting ReactApplication to MainApplication. i will wait @hmharshit 's answer impatiently 😃

Thanks for replies @saghul

Read more comments on GitHub >

github_iconTop Results From Across the Web

Receive messages in an Android app - Firebase - Google
Receive messages in an Android app ... Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want ......
Read more >
Firebase onMessageReceived not called when app in ...
This is working as intended, notification messages are delivered to your onMessageReceived callback only when your app is in the foreground.
Read more >
Cloud Messaging | FlutterFire
The Cloud Messaging package connects applications to the Firebase Cloud Messaging (FCM) service. You can send message payloads directly to devices at no ......
Read more >
Firebase Cloud Messaging for Android: Sending Push ...
The difference between receiving messages in the foreground and background of your app. How to use the data received in the messages.
Read more >
Restrictions on starting activities from the background
Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found