0

I have a question whereby how to let the app run actively behind the background despite the app is not being open by the user.

Taking for example, I want to receive notification or run a section of code despite the application is not open or active.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
jun
  • 33
  • 5
  • the only way to ensure the app is running in background is using foreground service (+ acquiring wakelock which prevents the device from going to doze mode) – Vladyslav Matviienko Jul 10 '19 at 07:14
  • Um, I have a firebase and my app. So my app will ask the User to create something and the User have to choose the date 2 days in advance and after choosing the date, for eg, today is 7/10/2019, user choose 9/10/2019. The app then will run the method to check if the current date is equal to 9/10/2019. If the condition is met, it will run a section of code automatically without the user opening the app. So with Foreground service i can achieve this ? – jun Jul 10 '19 at 07:56

2 Answers2

0

To handle notification while your app is closed, opened or in background, you can create a service extending

com.google.firebase.messaging.FirebaseMessagingService

public class YourMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(final RemoteMessage remoteMessage) {

      // Handle your notification here ...
      // Parse the notification, create your app channel,
      // set the activity to run when clicking the notification, ...

    }
}

You need to declare your service in your app's AndroidManifest.xml file:

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

You can look at these pages for more infos: https://firebase.google.com/docs/cloud-messaging/android/receive

https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService

Cannot find method createNotificationChannel(NotificationChannel)

matdev
  • 4,115
  • 6
  • 35
  • 56
0

To run any code in the background you need to create a service class, when your app goes into background start that service using "startService()" method, but you need also to create a notification telling the user that the app is running a service so the system will not kill your service. this is the notification :

Notification.Builder builder = new Notification.Builder(this)
            .setContentTitle("Title")
            .setContentText("content");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
             builder.setChannelId(channelId)

    //Make this notification ongoing so it can’t be dismissed by the user//

            .setOngoing(true)
            .setSmallIcon(R.drawable.notification_icon);
    startForeground(id, builder.build());

and inside the service class's onCreate write your logic. hope this helps

Kmelliti
  • 101
  • 7
  • Does this works when the app is killed ? Because in some situation wherby, the user will kill the app. So I want the app to be able to perform some insert firebase data codes after it reaches certain dates even if the app is killed. – jun Jul 10 '19 at 08:14
  • yes it does , use startIntent() in on destroy moethod and dont forget the notification, so the user will know that you are running a service – Kmelliti Jul 10 '19 at 14:29