4

I've implemented all the mechanism to send a push notification.

My app responds to the clicking the push message is not fixed. That is, when the user click the message, the status of my app (background or foreground) decides next step.

As you know, the method onMessageReceived() is called when the push message is received (even when the screen is off)

So, I want to insert some code that checks if the app is foreground or background in onMessageReceived().

Here's my codes below:

public void onMessageReceived(RemoteMessage remoteMessage) {
    String body = remoteMessage.getData().get("body");
    title = remoteMessage.getData().get("title");

    sendNotification(body);
    if(remoteMessage.getData().size() > 0){

    }
}

private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, Activity_CreateAccount.class);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.intro_logo)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
            .setPriority(Notification.PRIORITY_MAX);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    PowerManager pm = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE);

    PowerManager.WakeLock wakeLock =
            pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "");


    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

    wakeLock.acquire();

    if(wakeLock!=null){
        wakeLock.release();
        wakeLock = null;
    }
}
heesu
  • 83
  • 1
  • 8
  • 1
    check this http://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background – Quick learner Mar 21 '17 at 08:38

1 Answers1

5

As mentioned by @quicklearner you need to add an activity life cycle callback (manually).

A good SO post for the same can be found here

Summarizing:

  1. Create a custom application class, which extends Application

And add the following methods, something like this:

public class MyApplication extends Application {

  public static boolean isActivityVisible() {
    return activityVisible;
  }  

  public static void activityResumed() {
    activityVisible = true;
  }

  public static void activityPaused() {
    activityVisible = false;
  }

  private static boolean activityVisible;
}
  1. Also add it to the manifest file, as show in the post,

  2. Then for whichever activity you wish to check for,

inside the onResume method of that activity call the

MyApplication.activityResumed();

and inside the onPause call

MyApplication.activityPaused();

  1. Check for the state and perform logic based action

MyApplication.isActivityVisible()

Credit goes to the SO post!

Community
  • 1
  • 1
MadScientist
  • 2,134
  • 14
  • 27