kim jot
kim jot

Reputation: 74

Fetch old notification from fcm Android

I am using FCM for notification and it is working good in all scenarios in Foreground and background. My problem is when I am logout from my application and another user send me message, I am not getting any notification as I am logout which is fine, but when I again login in my application I want to receive that old unread notification so how to do this any help would be appreciated following is the code I used

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

    @Override
    public void onTokenRefresh() {

        // Get updated InstanceID token.
        try {
            String refreshedToken = FirebaseInstanceId.getInstance().getToken();

            Log.d(TAG, "Refreshed token: " + refreshedToken);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}

    public class MyFirebaseMessagingService extends FirebaseMessagingService {
        private static final String TAG = "FCM Service";


        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
            FirebaseDatabase.getInstance().setPersistenceEnabled(true);
            Log.e("remote", remoteMessage.getData().get("type") + "");
            Log.e("remoteMessage", remoteMessage.getData() + "");
            Log.e("remoteMessagebody", remoteMessage.getData().get("body") + "");

// handling other data also according to my app which i am not mentioning

    }
    }

I have used this code for fetching old notification but didnot worked

 FirebaseDatabase.getInstance().setPersistenceEnabled(true);

and in manifest

 <service
            android:name=".MyFirebaseMessagingService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".FirebaseIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>

and in gradle

compile 'com.google.firebase:firebase-messaging:9.2.1'

Upvotes: 1

Views: 1483

Answers (1)

AL.
AL.

Reputation: 37798

An FCM message has its own lifetime -- 4 weeks default, could be modified by setting time_to_live in your payload. After that time, the message would be discarded.

What you could do is implement that each notif is saved in a your Server DB, if it is not read yet and you detect that the user re-logs in, re-send them as push notifications, or simply retrieve them in your app and display them as notifications.

Upvotes: 1

Related Questions