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