当应用程序处于后台时,onMessageReceived 未执行
onMessageReceived is not executing when the app is in background
当我打开应用程序并收到通知时,会显示我的自定义通知。
但是当应用程序在后台时,它只使用默认通知设置。我正在尝试将通知图标从那个点更改为我的应用程序徽标。
我的 FirebaseMessagingService 代码如下
public static final String TG = "FCMService";
NotificationManagerCompat notificationManager;
@Override
public void onNewToken(@NonNull String s) {
Log.i(TG, "The token refreshed: " + s);
super.onNewToken(s);
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if(remoteMessage.getData().size() > 0)
{
Log.d(TG, "Message data payload : " + remoteMessage.getData());
}
if(remoteMessage.getNotification() != null)
{
Log.d(TG, "Message notification body : " + remoteMessage.getNotification().getBody());
}
Log.d(TG, "From : " + remoteMessage.getFrom());
Map<String, String> s = remoteMessage.getData();
sendNotification(s.get("message"));
}
private void sendNotification(String message) {
notificationManager = NotificationManagerCompat.from(this);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 ,intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_07c1b5098af03f95f3c3e8f7d461fb78)
.setContentTitle("Just In")
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.build();
notificationManager.notify(1, notification);
}
我的清单文件是
<service
android:name=".MyFirebaseMessagingService"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
There are two types of messages data messages and notification messages. Data messages are handled here in onMessageReceived whether
the app is in the foreground or background. Data messages are the type
traditionally used with GCM. Notification messages are only received
here in onMessageReceived when the app is in the foreground. When the
app is in the background an automatically generated notification is
displayed. When the user taps on the notification they are returned to
the app. Messages containing both notification and data payloads are
treated as notification messages. The Firebase console always sends
notification messages.
示例代码 :- 您需要像这样在通知负载中指定图标
$notification = array
(
'icon' => 'icon here',
'title' => 'title',
'body' => 'new msg',
'click_action' => 'action here'
);
注意:- 您需要在清单中添加这些才能使用默认通知图标,如下所示
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_launcher" />
// optional if required
<meta-data android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/notification_color" />
根据 Firebase 的参考,当您的应用程序在后台时,通知会传送到设备的系统托盘。用户点击通知会默认打开应用程序启动器。
在后台接收时同时包含通知和数据负载的消息。在这种情况下,通知会传送到设备的系统托盘,并且数据负载会在启动器的意图的额外部分传送 Activity。
Android Pie 和更新版本有一些限制。检查这个 reference
当我打开应用程序并收到通知时,会显示我的自定义通知。
但是当应用程序在后台时,它只使用默认通知设置。我正在尝试将通知图标从那个点更改为我的应用程序徽标。
我的 FirebaseMessagingService 代码如下
public static final String TG = "FCMService";
NotificationManagerCompat notificationManager;
@Override
public void onNewToken(@NonNull String s) {
Log.i(TG, "The token refreshed: " + s);
super.onNewToken(s);
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if(remoteMessage.getData().size() > 0)
{
Log.d(TG, "Message data payload : " + remoteMessage.getData());
}
if(remoteMessage.getNotification() != null)
{
Log.d(TG, "Message notification body : " + remoteMessage.getNotification().getBody());
}
Log.d(TG, "From : " + remoteMessage.getFrom());
Map<String, String> s = remoteMessage.getData();
sendNotification(s.get("message"));
}
private void sendNotification(String message) {
notificationManager = NotificationManagerCompat.from(this);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 ,intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_07c1b5098af03f95f3c3e8f7d461fb78)
.setContentTitle("Just In")
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.build();
notificationManager.notify(1, notification);
}
我的清单文件是
<service
android:name=".MyFirebaseMessagingService"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
There are two types of messages data messages and notification messages. Data messages are handled here in onMessageReceived whether the app is in the foreground or background. Data messages are the type traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app is in the foreground. When the app is in the background an automatically generated notification is displayed. When the user taps on the notification they are returned to the app. Messages containing both notification and data payloads are treated as notification messages. The Firebase console always sends notification messages.
示例代码 :- 您需要像这样在通知负载中指定图标
$notification = array
(
'icon' => 'icon here',
'title' => 'title',
'body' => 'new msg',
'click_action' => 'action here'
);
注意:- 您需要在清单中添加这些才能使用默认通知图标,如下所示
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_launcher" />
// optional if required
<meta-data android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/notification_color" />
根据 Firebase 的参考,当您的应用程序在后台时,通知会传送到设备的系统托盘。用户点击通知会默认打开应用程序启动器。
在后台接收时同时包含通知和数据负载的消息。在这种情况下,通知会传送到设备的系统托盘,并且数据负载会在启动器的意图的额外部分传送 Activity。
Android Pie 和更新版本有一些限制。检查这个 reference