Firebase Android NotificationCompat.PRIORITY_HIGH 在应用程序处于后台时无法运行

Firebase Android NotificationCompat.PRIORITY_HIGH not working while app is in background

我已经成功实施了 firebase。我唯一无法实现的是 NotificationCompat.PRIORITY_HIGH。它在应用程序处于前台时工作。在我的 PHP 代码中,我添加了优先级并将其值设置为高。虽然它似乎对 android 没有用。在我的 ios 应用程序中,高优先级正在运行。我无法弄清楚如何做到这一点。仅供参考:我知道有效负载数据和通知。 :) 谢谢。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";
Bitmap bitmap;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //Displaying data in log
    //It is optional
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Title: " + remoteMessage.getNotification().getTitle());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    String imageUrl = remoteMessage.getData().get("image");
    bitmap = getBitmapfromUrl(imageUrl);
    //Calling method to generate notification
    sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle());
}


private void sendNotification(String messageBody, String title) {
    Intent intent = new Intent(this, HomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);
    Log.e("inside notification", "method");
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_bicycle)
            .setColor(getResources().getColor(R.color.ColorPrimary))
            .setContentTitle(title)
            .setContentText(messageBody)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    if (Build.VERSION.SDK_INT >= 19) {
        notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody));
         notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(img))
    }
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Random rn = new Random();
    int result = rn.nextInt(100 + 1);
    notificationManager.notify(result, notificationBuilder.build());
}

这是我在 的回答。 问题在 onMessageReceived() 方法中 而不仅仅是优先级

从你的问题我可以看出你也在从服务器发送通知负载,这是这个问题的根本原因


onMessageReceived()方法如果应用程序处于后台或被杀死将不会被调用 ] 并且如果发送的消息 同时包含数据和通知负载。

当应用程序不是 运行 时,您无论如何都会收到通知。但是如果你想通过 onMessageReceived() 拦截数据,那么你将不得不创建一个自定义应用程序服务器,它将 仅发送数据负载 到 fcm 端点。

像这样:

 {
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
     "Title" : "Title for Notification",
     "body" : "Notification body can go here",
     "Room" : "Something extra"
   },
 }

希望这能解决您的问题。 (可以参考fcm documentation here里面说的)

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.

This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

所以,永远不会调用 onMessageReceived()!如果您想在 onMessageReceived 中手动处理内容,请按照此答案中的说明进行操作。