Mahmoud Nasr
Mahmoud Nasr

Reputation: 11

how to get all firebase push notifications after a connection is established

a server is sending data (push) notification and as client side , i'm working with FirebaseMessagingService and onMessageRecieved method to make notifications , and it's working good in foreground and background , but when my phone is off (no internet) and a connection is established , the last notification only is delivered to the android phone .

public class MyFirebaseMessagingService extends MessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> data  = remoteMessage.getData();
    String title = remoteMessage.getNotification().getTitle();
    String message = remoteMessage.getNotification().getBody();
    String imageUrl = (String) data.get("image");
    String action = (String) data.get("action");
    Log.i(TAG, "onMessageReceived: title : "+title);
    Log.i(TAG, "onMessageReceived: message : "+message);
    Log.i(TAG, "onMessageReceived: imageUrl : "+imageUrl);
    Log.i(TAG, "onMessageReceived: action : "+action);

    if (remoteMessage.getData().isEmpty()){
        Log.e(TAG , "Empty");
        showNotification(remoteMessage.getNotification().getTitle() , remoteMessage.getNotification().getBody());
    }
    else {
        showNotification(remoteMessage.getData());
    }
}

private void showNotification(Map<String, String> data) {
    String title = data.get("title").toString();
    String body = data.get("body").toString();
    Intent intent = new Intent(this, UserHomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "com.example.thelawyerhouse.test";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID , "notification"
                , NotificationManager.IMPORTANCE_DEFAULT);
        notificationChannel.setDescription("LawyerHome Channel");
        notificationChannel.enableLights(true);
        notificationManager.createNotificationChannel(notificationChannel);
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this , NOTIFICATION_CHANNEL_ID);
    builder.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_notifications_black_24dp)
            .setContentTitle(title).setContentText(body).setContentInfo("info")
            .setContentIntent(pendingIntent);

    notificationManager.notify(new Random().nextInt() , builder.build());
}

private void showNotification(String title, String body) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "com.example.thelawyerhouse";
    Intent intent = new Intent(this, UserHomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID , "notification"
        , NotificationManager.IMPORTANCE_DEFAULT);
        notificationChannel.setDescription("LawyerHome Channel");
        notificationChannel.enableLights(true);
        notificationManager.createNotificationChannel(notificationChannel);
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this , NOTIFICATION_CHANNEL_ID);
    builder.setAutoCancel(true)
    .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_notifications_black_24dp)
    .setContentTitle(title).setContentText(body).setContentInfo("info").setContentIntent(pendingIntent);
    notificationManager.notify(new Random().nextInt() , builder.build());
}


@Override
public void onNewToken(@NonNull String s) {
    super.onNewToken(s);
    Log.d("TOKEN" , s);
}

}

Upvotes: 1

Views: 416

Answers (1)

from56
from56

Reputation: 4127

That seems the expected behavior, if your app is not active then the notification is sent directly to the system tray and your onMessageReceived() is not called

https://firebase.google.com/docs/cloud-messaging/android/receive

Upvotes: 1

Related Questions