0

I am trying to Show notifications only when the app is closed, and I managed to that by the following code

public class AppMain extends Application {

public static boolean isInForeGround;
    @Override
    public void onCreate() {
        super.onCreate();
        isInForeGround = true;
    }

    @Override
    public void onTrimMemory(int level) {
        if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
            isInForeGround = false;
        }
    }
}

Then where I Receive the Notification message i do the following;

     @Override
            public void onMessageReceived(RemoteMessage remoteMessage) {
                if (remoteMessage.getNotification() != null) {
                    Logging.log("body" + remoteMessage.getNotification().getBody());
                    Logging.log("Title" + remoteMessage.getNotification().getTitle());

                    if (isInForeGround) {
                        Logging.log("The app is in the foreground");
                        return;
                    } else {
                        if (remoteMessage.getNotification().getTitle().equalsIgnoreCase("New Chat Added")) {
                            sendNotification(remoteMessage.getNotification(), ChatActivity.class);
                        } else if (remoteMessage.getNotification().getTitle().equalsIgnoreCase("New Announcement Added")) {
                            sendNotification(remoteMessage.getNotification(), AnnouncementActivity.class);
                        } else {
                            sendNotification(remoteMessage.getNotification(), NotificationsActivity.class);
                        }
                    }

                } else {
                    Logging.log(" Notification is null ");
                }
            }

And the function creating the notification is;

private void sendNotification(RemoteMessage.Notification messageBody, Class activityClass) {

    Intent intent = new Intent(this, activityClass);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(messageBody.getTitle())
            .setContentText(messageBody.getBody())
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    notificationBuilder.setSmallIcon(getNotificationIcon());


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

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

}
 private int getNotificationIcon() {
        boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
        return useWhiteIcon ? R.mipmap.notification_ : R.mipmap.notification_;
    }

The issue is while the app is closed, The notification icon changes to the app launcher, and it get created for multiple times, the FLAG_UPDATE_CURRENT and FLAG_CANCEL_Current Never works

Tested the solution while the app is in the foreground and it worked fine with the wright icon and updated the previous notification, I need to know the reason of the issue and a solution of possible.

aya salama
  • 903
  • 1
  • 10
  • 29

0 Answers0