0

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.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
Maximus
  • 189
  • 1
  • 3
  • 15
  • 1
    see this https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase – Bunny Oct 31 '18 at 12:00
  • I have seen that link already before, thanks, but i need to do even when app is removed from task or killed. – Maximus Oct 31 '18 at 12:06

2 Answers2

1

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>
Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
0

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.

jaydeep_gedia
  • 484
  • 2
  • 4
  • 13