1

I want to call some service from my application when application receives a notification when the application is either in the background or is killed.

Earlier, I was using a WakefulBroadcastReceiver for handling such notifications, but it is deprecated now. Can someone please help with it?

Thanks in advance.Cheers

tomerpacific
  • 4,704
  • 13
  • 34
  • 52
Yogesh Katkar
  • 369
  • 1
  • 4
  • 16
  • Possible duplicate of [WakefulBroadcastReceiver is deprecated](https://stackoverflow.com/questions/47217345/wakefulbroadcastreceiver-is-deprecated) – tomerpacific Jan 07 '19 at 09:47

3 Answers3

0

Use JobScheduler

As of Android O, background check restrictions make this class no longer generally useful. (It is generally not safe to start a service from the receipt of a broadcast, because you don't have any guarantees that your app is in the foreground at this point and thus allowed to do so.) Instead, developers should use android.app.job.JobScheduler to schedule a job, and this does not require that the app hold a wake lock while doing so (the system will take care of holding a wake lock for the job).

Alex
  • 9,102
  • 3
  • 31
  • 35
0

just create a service extend FirebaseMessagingService and override onMessageReceived

class MyFirebaseMessagingService : FirebaseMessagingService() {
  override fun onMessageReceived(remoteMessage: RemoteMessage?) {
    Log.d(TAG, "From: ${remoteMessage?.from}")
  }
}

add service to your manifest

<service android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

offical document

vuhung3990
  • 6,353
  • 1
  • 45
  • 44
0

Instead of WakefulBroadcastReceiver you should extend regular BroadcastReceiver.

Then, in onReceive you can enqueue work using JobIntentService

Singed
  • 1,053
  • 3
  • 11
  • 28