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

时光总嘲笑我的痴心妄想 提交于 2019-12-24 18:45:48

问题


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.


回答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>



回答2:


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.



来源:https://stackoverflow.com/questions/53082790/how-to-get-fcm-notification-data-when-app-is-not-in-task-or-killed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!