android - Firebase Android NotificationCompat.PRIORITY_HIGH 在应用程序处于后台时无法正常工作

标签 android firebase android-notifications firebase-cloud-messaging

我已经成功实现了 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());
}

最佳答案

这是我来自 another similar post 的回答. 问题在 onMessageReceived() 方法中而不仅仅是优先级

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


onMessageReceived()方法不会被调用如果应用程序处于后台或被终止并且如果发送的消息包含数据和 NOTIFICATION 负载。

当应用程序未运行时,您无论如何都会收到通知。但是,如果您想通过 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 中手动处理内容,请按照此答案中的说明进行操作。

关于android - Firebase Android NotificationCompat.PRIORITY_HIGH 在应用程序处于后台时无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39792275/

相关文章:

android - 在 Fragment Kotlin 中调用 DialogFragment

java - 根据应用程序是在前台还是后台,通知显示不同

android - 使用 Android L Notification.MediaStyle 添加终止播放按钮

android - 我应该使用什么权限来接收 android 通知?

java - 无法读取Firebase数据库,数据返回null

java - 如何在 RelativeLayout 中将按钮居中放置在 FrameLayout 的底部

android - 如何从 ADB 获取可用/空闲内存?

java - 如何处理Android应用程序的更多用户(拥有自己的帐户)?多线程?

android - 如何使用 Firebase Messaging 发送一对一消息

ios - Geofire/火力基地.indexOn