本文介绍了当应用不在任务中或被杀死时,如何获取FCM通知数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从android中的FCM通知中获取数据并将其存储在本地,但是问题是我只能在应用程序位于前台然后 onMessageRecieved
时才能这样做或当用户点击通知时被调用。我想在用户收到通知且应用未运行时(甚至在后台或前台)都无法获取通知的数据。请提出一些建议。预先谢谢您。
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.
推荐答案
您可以使用
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>
这篇关于当应用不在任务中或被杀死时,如何获取FCM通知数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!