2

hey all i got a tough problem and need advice. I have constructed a notification manually after recieving a FCM data payload. This is how the notification gets created both in foreground and background since its a data payload:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {


    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {

        String msg = remoteMessage.getData().get("message");
        sendNotification(msg);
    }

private PendingIntent createIntent(String msg) {
    Intent intent = new Intent(this, SportsActivity.class);
    Bundle b = new Bundle();
        b.putString(Constants.KEY_GO_TO_TAB, Constants.KEY_DASHBOARD_HOCKEY_SCORE_TAB);
    intent.putExtras(b);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);
    return pendingIntent;
}

private void sendNotification( String messageBody) {
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    android.support.v4.app.NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(messageBody))
            .setContentText(messageBody)
            .setColor(ContextCompat.getColor(this, R.color.hockey_brand))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(createIntent(messageBody));

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

    notificationManager.notify(0, notificationBuilder.build());
}

}

it seems to function fine. The issue im having is i want the notification to NOT SHOW in when the app is in the foreground. is there not a way to do this without scanning the activity task ? as this requires a permission

i've read this article but this how to know you've went from foreground to background. also android.permission.GET_TASKS is deprecated and REAL_GET_TASKS permission is not for third party either. I simply want to know at any given time that the user is either in foreground or background so i know if i should show a notification or not. I wonder if firebase itself has something. When you send a "Notification payload" from the firebase console if the app is not in the foreground is does not show in the notification panel so there should be a way to do this.

Community
  • 1
  • 1
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • maybe [this SO post](http://stackoverflow.com/a/39589766/5015207) is helpful? – Bö macht Blau Jan 27 '17 at 18:28
  • thanks even though android.permission.GET_TASKS is a deprecated permission i believe. tell me if i am wrong. I dont even see it on the list of permssions anymore. and im supporting api 19 so i cant use getAppTasks(); do you know if android.permission.GET_TASKS is a dangerous permission ? – j2emanue Jan 27 '17 at 18:47
  • The code snippet by Juozas Kontvainis does not seem to need any permission. I suppose it's because it just tries to obtain information about its own application. – Bö macht Blau Jan 27 '17 at 20:14
  • thank you so much for helping me with this. i was focusing so much on GET_TASKS that i did not see this one which so far works great !. its actually a hidden gem , no ones seeing this way i wonder if there is a downside. but thanks. – j2emanue Jan 28 '17 at 00:23

1 Answers1

1

Alternatively you can choose not to show your notification when your app is in foreground by implementing Application.ActivityLifecycleCallbacks in your Application.

Then you can check if your app is in foreground or background.

Source

hitesh
  • 378
  • 4
  • 12