Maximus
Maximus

Reputation: 199

How to get FCM notification data when app is not in task or killed?

I need to get data from FCM notifications in android and store them locally, but the problem is I am only able to do that when app is in foreground and then onMessageRecieved is called or when user taps on notification. I want to get notification's data when user gets notification and app is not running, not even in background or foreground. Please suggest something. Thank you in advance.

Upvotes: 0

Views: 1906

Answers (2)

Sagar Zala
Sagar Zala

Reputation: 5144

You can use BroadcastReceiver

public class FirebaseDataReceiver extends BroadcastReceiver {

    private final String TAG = "FirebaseDataReceiver";

    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Set<String> keys = intent.getExtras().keySet();

            for (String key : bundle.keySet()) {
                Object value = bundle.get(key);
                // You can use key and values here
            }
        }
    }
}

Manifest.xml

<application>

    ............

    <receiver
        android:name="PackageName.FirebaseDataReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </receiver>

    ............

</application>

Upvotes: 1

jaydeep_gedia
jaydeep_gedia

Reputation: 494

By using FCM console you can only send notification messages. notification messages can be handled by the onMessageReceived method in foregrounded application and deliver to the device’s system tray in backgrounded application. User taps on notification and default application launcher will be opened. if you want to handle notification in every state of application you must use data message and onMessageReceived method.

Refer this for more info.

Upvotes: 0

Related Questions