Java 如何在Android应用程序中处理/处理Firebase Cloud Messaging的数据有效负载(包括通知)?
我对接收带有通知和数据负载的Firebase消息有疑问。该公司表示,这些数据将“以额外的目的”到达 我的问题是哪种意图(或活动)?当用户将应用程序切换到后台时,会出现一个屏幕,在该屏幕上用户停止了操作。那么,我是否需要尝试检索我应用程序中所有意图/活动的额外信息 一旦应用程序出现在前台,我应该在哪里以及如何编写代码来检索数据负载 谢谢 增加:Java 如何在Android应用程序中处理/处理Firebase Cloud Messaging的数据有效负载(包括通知)?,java,android,firebase,firebase-cloud-messaging,Java,Android,Firebase,Firebase Cloud Messaging,我对接收带有通知和数据负载的Firebase消息有疑问。该公司表示,这些数据将“以额外的目的”到达 我的问题是哪种意图(或活动)?当用户将应用程序切换到后台时,会出现一个屏幕,在该屏幕上用户停止了操作。那么,我是否需要尝试检索我应用程序中所有意图/活动的额外信息 一旦应用程序出现在前台,我应该在哪里以及如何编写代码来检索数据负载 谢谢 增加: 我的意思是,我一直有10多个活动,当应用程序完成后,会有更多的活动。那么,我是否必须检索所有活动的额外数据,以查看应用程序是否已使用推送数据负载重新打开
我的意思是,我一直有10多个活动,当应用程序完成后,会有更多的活动。那么,我是否必须检索所有活动的额外数据,以查看应用程序是否已使用推送数据负载重新打开 您必须扩展
FirebaseMessagingService
类
并重写onMessageReceived
方法
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if (/* Check if data needs to be processed by long running job */ true) {
// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
scheduleJob();
} else {
// Handle message within 10 seconds
handleNow();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification
method below.
}
确保在清单中注册服务。您必须扩展
FirebaseMessagingService
类
并重写onMessageReceived
方法
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if (/* Check if data needs to be processed by long running job */ true) {
// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
scheduleJob();
} else {
// Handle message within 10 seconds
handleNow();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification
method below.
}
确保您在清单中注册了服务。在您在问题中链接的文档中,它说明: 具有通知和数据有效负载(包括后台和后台)的消息 前景。在这种情况下,通知将传递给 设备的系统托盘,数据有效负载在extras中交付 您的启动程序活动的意图 使用类别启动器在清单中指定启动器活动。例如:
<activity
android:name="com.example.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
按如下方式定义意图过滤器:
<activity android:name=".MyFcmNotificationActivity">
<intent-filter>
<action android:name="com.example.FCM_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
为了稍微澄清一下文档,当收到消息时,数据有效载荷没有传递给活动;当用户单击通知时,它将被发送。在您在问题中链接的文档中,它声明: 具有通知和数据有效负载(包括后台和后台)的消息 前景。在这种情况下,通知将传递给 设备的系统托盘,数据有效负载在extras中交付 您的启动程序活动的意图 使用类别启动器在清单中指定启动器活动。例如:
<activity
android:name="com.example.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
按如下方式定义意图过滤器:
<activity android:name=".MyFcmNotificationActivity">
<intent-filter>
<action android:name="com.example.FCM_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
为了稍微澄清一下文档,当收到消息时,数据有效载荷没有传递给活动;当用户单击通知时,它就会被发送。我已经这样做了。但文档中说,如果有效负载同时包含数据和通知,它将不会到达onMessageReceived,而是“在意图之外”。我已经这样做了。但是文档中说,如果有效负载同时包含数据和通知,它将不会到达onMessageReceived,而是“在意图之外”。我更新了答案,以包含如何覆盖默认行为的描述。我更新了答案,以包含如何覆盖默认行为的描述。