I have successfully handled notifications when app is in foreground by FirebaseMessagingService class. which will generate notification from app using this code:
private void sendNotification(String type, String typeID, String messageBody, String title) {
Intent intent = new Intent(this, SplashScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(getNotificationIcon())
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
try {
count = MazkaraPref.getInstance().getInt(getApplicationContext(), MazkaraPref.PreferenceKey.notification_count, 0);
count++;
Log.e("count", ":" + count);
MazkaraPref.getInstance().saveInt(getApplicationContext(), MazkaraPref.PreferenceKey.notification_count, count);
} catch (Exception e) {
e.printStackTrace();
}
}
This way I have handled Notification count when application is running in foreground. But when app is in backgorund we have no control to generate notification or count notification. So if i want to know how many notifications are pending in status bar or how many notifications are arrived at the time of SplashScreen open. How can i achieve this Notification count.I have tried code above but it works if app is running only. I am not getting it how to get pending notification count.