0

I have made a push notification service in firebase and sending data notifications and receiving it in onMessageReceived(RemoteMessage remoteMessage) very well,

I am well aware when to send which type of notifications and that's why I have used data notifications to get it when the app is killed.

But in my case, I do not want to send any notification when app is in Foreground mode, as onMessageReceived(RemoteMessage remoteMessage) will always get called when the app is in Foreground mode.

How to prevent this situation?

My notification format is this..

{
   "to" : "cGPhwcLbe5Y:APA91bFVAR6n9GxbDBovzd8X9nMBvKZ4Z098FzmkMGGUAuMhdps4uEof8mppUh7vrFTAsdFGzyurHa4gQ1nWgP-ncaN53ZtHziLi8YdHMEOGvtx6pA7ttCY5QhE5XKdFZ2QfX",
   "data" : {
    "message":"hi"
 }

}
Abdul Rehman Yawar Khan
  • 1,088
  • 3
  • 17
  • 40
TapanHP
  • 5,969
  • 6
  • 37
  • 66
  • @AL. please provide appropriate link man – TapanHP Jan 19 '17 at 07:21
  • 1
    whoops. Sorry. Copied the wrong link. Here it is -- http://stackoverflow.com/q/8489993/4625829 – AL. Jan 19 '17 at 07:28
  • delete that confusing comment @AL. and second thing have you seen comments on that answer? I have already checked that answer but it's not an efficient way, also GET_TASKS permission is deprecated above api 21, and it is necessary to make that answer work – TapanHP Jan 19 '17 at 07:33
  • 2
    I referenced it for the idea. Because unless you find a way to do something similar to it, I don't think there is any other way for you to *prevent* showing notifications. – AL. Jan 19 '17 at 07:35
  • 1
    @TapanHP bro understand what others are trying to explain you instead of what already in your mind.. AL's suggested link will also work after API 21 – Sanket Kachhela Jan 19 '17 at 07:45
  • but after API 21 GET_TASKS permission is deprecated @SanketKachhela – TapanHP Jan 19 '17 at 07:49
  • both suggested answer aren't using that permission. – Sanket Kachhela Jan 19 '17 at 07:51
  • @AL. can we do something from Firebase side, like is there any method that can handle this? – TapanHP Jan 19 '17 at 07:52
  • @SanketKachhela well the same link provided another answer could be the solution, can you check it for me? – TapanHP Jan 19 '17 at 07:55
  • So which way will preferable more? yours provided link or Al.'s ? @SanketKachhela – TapanHP Jan 19 '17 at 07:58
  • 1
    Nope. You're well aware that `onMessageReceived()` will always be called for `data`-only payloads even when the app is in foreground/background. This has to be checked on client side. Unless you can check if user is *online* (app foreground), then don't send push notifications for a while. Only if you've determined if the user is maybe *idle/offline*, that's when you send push notifications. – AL. Jan 19 '17 at 08:02
  • okay cool, I will check out the link you have provided @AL. – TapanHP Jan 19 '17 at 08:03
  • No worries. Good luck. If ever you find a workaround, add it in as answer for the community to see. – AL. Jan 19 '17 at 08:05

2 Answers2

4

There is 2 different type of message you can send from your server.

enter image description here

Where Notification message is handle by firebase itself.

Data message is custom payload, where you need to manage notification from your self.

so you need to use data key in your server script.

and just check that which app is running in top stack from this answer https://stackoverflow.com/a/27689352/942224

private boolean isAppInForground(Context context, String packageName){
  String[] activePackages;
  ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
   activePackages = getActivePackages(am);

   if (activePackages != null) {
       return activePackages[0].equals(packagName)
   }
    return false;
}



private String[] getActivePackages(ActivityManager mActivityManager) {
final Set<String> activePackages = new HashSet<>();
final List<ActivityManager.RunningAppProcessInfo> processInfos = mActivityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
            if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                activePackages.addAll(Arrays.asList(processInfo.pkgList));
            }
        }
        return activePackages.toArray(new String[activePackages.size()]);
    }
Community
  • 1
  • 1
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
0

Notifications are delivered to your onMessageReceived callback only when your app is in the foreground. If your app is in the background or closed then a notification message is shown in the notification center. If you want to prevent that onMessageReceived is being called when your app is in the foreground. just get rid of FirebaseMessagingService from your app.

Check the docuemnt: Send your First Message to a Backgrounded App

wonsuc
  • 3,498
  • 1
  • 27
  • 30