0

I know that, when the app is killed, it can't shows any Firebase notification... or this is what happens with my new app: I kill the app, The new notifications don't come up as well.

Now, I want to get rid of this problem in a new way: I want to retrieve, when my app starts, all the notifications the app "forgot" to see.

Here's my code

IdService

  public class FirebaseIDService extends FirebaseInstanceIdService {
    private static final String TAG = "FirebaseIDService";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);
    }

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

and the message class

   public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
        notifies(remoteMessage.getNotification().getBody());
    }

    private void notifies(String body){
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(MyFirebaseMessagingService.this)
                        .setSmallIcon(android.R.drawable.ic_menu_help)
                        .setContentTitle("Congratulations!")
                        .setContentText(body);
        Notification mNot= mBuilder.build();
// Display
        NotificationManager mNotMan = (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);
        mNotMan.notify(1,mNot);
    }

}

What can you suggest to me? Thanks for all the answers :D

Julian Schmuckli
  • 3,681
  • 11
  • 37
  • 64
Paolo Mazza
  • 87
  • 11
  • If you dont set a time to live time on your message if your app does not get it then it never will. You cannot "get all missed messages" there is no such thing – tyczj Jun 12 '17 at 15:02
  • Ok, so I have to set a time to live, right? On the Firebase console I set the message to be online for four weeks, but it doesn't work... – Paolo Mazza Jun 12 '17 at 15:12
  • This is odd. The behavior of FCM is for it to send the message(s) as soon as the app/device is available. Is this the behavior for all the devices you tested it with? – AL. Jun 13 '17 at 02:42
  • Yeah, for every device I tested (one Cubox and a Honor7) – Paolo Mazza Jun 13 '17 at 09:00
  • I can receive it only when the app is running, or while it's running in the background – Paolo Mazza Jun 13 '17 at 09:02

1 Answers1

0

IMHO the best way to do this is create a notification center server side.

So , every time when you send a push notification save it on server. When your app is starting up get push notifications list remotely.

an_droid_dev
  • 1,136
  • 14
  • 18
  • Thank you for your answer! On the emulator (Nexus 5) it works, on the physical phones (an Huawei and a CUBOX) it doesn't work... – Paolo Mazza Jun 15 '17 at 14:19
  • Sorry, I forgot to say I not implemented your solution, but my answer was referred to the normal functioning of Firebase's notifications. – Paolo Mazza Jun 15 '17 at 14:31
  • I have not to implement it, however, it's a problem of physical devices, so I don't know why. Thank you for your effort, anyway! – Paolo Mazza Jun 15 '17 at 14:31
  • Ok , but what happen? Have you issue on device registration? – an_droid_dev Jun 15 '17 at 14:32
  • No. On my emulator, I receive the messages, even if the app is swiped. On my two physical devices no, they receive it only if the app is running or is in background, and they don't receive the notification neither if I open the app after I sent the notification (thing that doesn't happen with my emulator). – Paolo Mazza Jun 15 '17 at 14:35
  • @PAMAZ yes, i encountered my self this "issue" implementing Firebase Push , see this question https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase – an_droid_dev Jun 15 '17 at 15:00
  • Ok, I'll refer to you link. Thank you! – Paolo Mazza Jun 15 '17 at 15:24
  • Glad to help you! – an_droid_dev Jun 15 '17 at 15:28