当应用程序处于后台或被杀死时 FCM onMessageReceived() 未调用
When app is in background or killed FCM onMessageReceived() not called
当应用程序处于后台或被杀死时 onRecive onMessageReceived() 未调用,我只收到 json 通知。
下面是我的服务 class
public class AppFireBaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Check if message contains a data payload.
else if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("body"));
}
}
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
PreferenceHelper preferenceHelper = new PreferenceHelper(this);
preferenceHelper.putString(Constants.FCM_TOKEN, token);
}
public void sendNotification(String title, String messageBody) {
try {
ObjectMapper mapper = new ObjectMapper();
Notification notification = mapper.readValue(messageBody, Notification.class);
Intent intent = null;
if (notification.getPostType().equalsIgnoreCase("message")) {
intent = new Intent(this, MessageActivity.class);
intent.putExtra(Constants.LOGIN_ID, notification.getLoginIdEnc());
} else {
intent = new Intent(this, HomeActivity.class);
}
Toast.makeText(this, notification.toString(), Toast.LENGTH_SHORT).show();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(notification.getMessage())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
}
}
}
我试过调试它,但也没有打印日志。如果打开应用程序并且 运行 获得正确的通知并且通知已正确构建。当我尝试使用应用后台通知消息设置 json 响应时。
如果推送通知在负载中有通知部分并且您的应用程序在后台,则永远不会调用 onMessageReceived。通知由 firebase 自动创建和显示。没有办法拦截它并修改通知或为此做任何其他操作。只有在点击通知后,您的应用才会获得控制权。
确保在所有情况下(前台、后台和应用程序终止)调用 onMessageReceived 的唯一方法是从 firebase 发送纯数据负载。
请查看我的 answer 类似问题以了解更多详情。
当应用程序处于后台或被杀死时 onRecive onMessageReceived() 未调用,我只收到 json 通知。 下面是我的服务 class
public class AppFireBaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Check if message contains a data payload.
else if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("body"));
}
}
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
PreferenceHelper preferenceHelper = new PreferenceHelper(this);
preferenceHelper.putString(Constants.FCM_TOKEN, token);
}
public void sendNotification(String title, String messageBody) {
try {
ObjectMapper mapper = new ObjectMapper();
Notification notification = mapper.readValue(messageBody, Notification.class);
Intent intent = null;
if (notification.getPostType().equalsIgnoreCase("message")) {
intent = new Intent(this, MessageActivity.class);
intent.putExtra(Constants.LOGIN_ID, notification.getLoginIdEnc());
} else {
intent = new Intent(this, HomeActivity.class);
}
Toast.makeText(this, notification.toString(), Toast.LENGTH_SHORT).show();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(notification.getMessage())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
}
}
}
我试过调试它,但也没有打印日志。如果打开应用程序并且 运行 获得正确的通知并且通知已正确构建。当我尝试使用应用后台通知消息设置 json 响应时。
如果推送通知在负载中有通知部分并且您的应用程序在后台,则永远不会调用 onMessageReceived。通知由 firebase 自动创建和显示。没有办法拦截它并修改通知或为此做任何其他操作。只有在点击通知后,您的应用才会获得控制权。
确保在所有情况下(前台、后台和应用程序终止)调用 onMessageReceived 的唯一方法是从 firebase 发送纯数据负载。
请查看我的 answer 类似问题以了解更多详情。