简体   繁体   English

当应用不在任务中或被杀死时,如何获取FCM通知数据?

[英]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. 我需要从android中的FCM通知中获取数据并将其存储在本地,但是问题是我只能在应用程序处于前台然后调用onMessageRecieved或用户点击通知时做到这一点。 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. 先感谢您。

You can use BroadcastReceiver 您可以使用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 的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>

By using FCM console you can only send notification messages. 通过使用FCM控制台,您只能发送通知消息。 notification messages can be handled by the onMessageReceived method in foregrounded application and deliver to the device's system tray in backgrounded application. 通知消息可以由前台应用程序中的onMessageReceived方法处理,并在后台应用程序中传递到设备的系统托盘。 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. 如果要在应用程序的每种状态下处理通知,则必须使用数据消息和onMessageReceived方法。

Refer this for more info. 请参阅以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 android 应用程序处于终止状态时,如何从推送通知(使用 FCM)中获取“数据有效负载”? - How can I get “data payload” from push notification (using FCM) when android app is in killed state? 当应用程序在后台或在 FCM 服务中被杀死时如何处理多个通知 - How to handle multiple notification when app in background or killed in FCM services 应用程序完全终止时未收到 FCM 通知 - Not getting FCM notification when app is completely killed 应用程序被杀死时 FCM 通知不会触发 - FCM Notification not firing when app is killed 应用程序被杀/刷出时未收到FCM推送通知 - FCM push notification not received when app is killed/swiped out 当应用被多个 Firebase 项目杀死时未收到 FCM 通知 - FCM notification not received when app is killed with multiple Firebase projects 应用程序被杀死/刷出时的 FCM 推送通知 - FCM push notification when app is killed/swiped out 应用程序关闭或被杀死时未收到 Android FCM 通知 - Android FCM notification is not received when app is close or Killed 当应用被终止时,OPPO和realme中未收到FCM通知 - When app is killed FCM notification not received in OPPO and realme 应用被终止后,点击FCM通知即可打开浏览器 - Open browser on click of FCM notification when app is killed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM