2

I have a messenger application and now I'm doing push notifications part. I'm using FCM (Firebase Cloud Messaging)for this. Now I have a problem. For example introduce 2 users (user1 and user2). If user1 writes me, I'm getting notification with FCM and show it. When user2 writes me, I'm creating new notification for it. But when user1 writes me again and I don't removed my user1 notification, I need to update the notification, else create new. I think you understand me. Now I am stuck at update part, because every time I'm create a new notification on message income.

I searched and found something. It says I have to use NotificationListenerService for this and use getActiveNotifications() for getting all active notifications in status bar.

So I tried something, but I'm getting null. I enabled this application in Notification Access, so I think this is not the problem. I registered my services in manifest file.

Here is my code, where I created my FCM service. If I done something wrong in my code, please fix it.

public class FireBaseService extends FirebaseMessagingService {

private static final String TAG = "FireBaseMessagingServiceTag";

@SuppressLint("LongLogTag")
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // Handle FCM messages here.

    Intent service = new Intent(FireBaseService.this, NotificationService.class);
    startService(service);

    //I think I'm doing wrong here.
    NotificationService notificationService = new NotificationService();

    StatusBarNotification[] activeNotifications = notificationService.getActiveNotifications();

    Log.d(TAG, "Array: " + Arrays.toString(activeNotifications));

    //show notification
}
}

And here is the NotificationService class.

public class NotificationService extends NotificationListenerService {

@Override
public IBinder onBind(Intent intent) {
    return super.onBind(intent);
}

@Override
public void onListenerConnected() {
    super.onListenerConnected();
}

@Override
public void onNotificationPosted(StatusBarNotification sbn){
    // Implement what you want here
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn){
    // Implement what you want here
}

@Override
public StatusBarNotification[] getActiveNotifications() {
    return super.getActiveNotifications();
}
}

So this is all I done. Can you help me? Thank you and please fix my code, if I done something wrong instead of giving bad vote to my question.

Here is the notification part.

private void showNotification(String title, String body) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "com.example.fcmnotification.FireBaseService";

    if(notificationManager != null) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(
                    NOTIFICATION_CHANNEL_ID, "NotificationManager",
                    NotificationManager.IMPORTANCE_DEFAULT);

            notificationChannel.setDescription("My Channel");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        notificationBuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher_notification)
                .setContentTitle(title)
                .setContentText(body)
                .setContentInfo("info");

        notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Hayk Mkrtchyan
  • 2,835
  • 3
  • 19
  • 61

1 Answers1

0

You don't need a NotificationListenerService. Just get the active notifications from the NotificationManager

NotificationManager nm = (NotificationManager)ctx.getSystemService(Activity.NOTIFICATION_SERVICE);
StatusBarNotification[] statusNotifs = null;
try {
    statusNotifs = nm.getActiveNotifications();
} catch (Throwable t) {
    // it can crash
}
if (statusNotifs != null) for (StatusBarNotification n : statusNotifs) {
    Notification notif = n.getNotification();
    try {
        String ntext = notif.extras.getCharSequence(Notification.EXTRA_TEXT).toString();
        // do something
    } catch (Throwable t) {
        // can crash
    }
}
grebulon
  • 7,697
  • 5
  • 42
  • 66