Android 通知不';我不在前台工作

Android 通知不';我不在前台工作,android,firebase,push-notification,firebase-cloud-messaging,foregroundnotification,Android,Firebase,Push Notification,Firebase Cloud Messaging,Foregroundnotification,如果应用程序在后台但在前台不起作用,则我的通知会起作用 这是我的MyFirebaseMessagingService类: public class MyFirebaseMessagingService extends FirebaseMessagingService { public static final String TAG = "MessagingService"; @Override public void onMessageReceived(RemoteM

如果应用程序在后台但在前台不起作用,则我的通知会起作用

这是我的MyFirebaseMessagingService类:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    public static final String TAG = "MessagingService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        String title = remoteMessage.getNotification().getTitle();
        String message =  remoteMessage.getNotification().getBody();

        String uid = remoteMessage.getData().get("uid");
        String click_action = remoteMessage.getNotification().getClickAction();

        Log.d(TAG, "onMessageReceived: "+title + message + uid+" "+click_action);


        Intent intent = new Intent(click_action);
        if (click_action.equals("com.example.android.ProfileFriends")){
            intent.putExtra("uid",uid);
        }else if (click_action.equals("com.example.android.ChatActivity")){

        }
      //  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle(title);
        notificationBuilder.setContentText(message);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher_event);
        notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
        notificationBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());



    }

}

请问,我如何解决此问题?

FCM对应用程序状态有不同的行为(前台和后台/已终止)。 根据您的用例,您应该通过从服务器发送的有效负载来处理这个问题

从服务器发送的消息必须以“通知”或“数据”格式从仪表板或服务器端api发送。 注意:从firebase dashobard,您只能发送“通知”正文,而不能发送数据。在这种情况下,FCM将直接显示notif,而不回调您的应用程序

服务器端 以下是示例格式:

通知类型格式 注意:Android系统默认情况下会在通知托盘中显示通知,您无需显示

 { 
    "to": "your_token_id",
     "notification" : {
             "title" : "FCM Notification title!",
             "body" : "FCM Notification subtext!",
             "content_available" : true,
             "priority" : "high"
     }
}
数据格式(用于在应用程序中、前台和后台接收回调) 注意:您必须自己处理回调和显示notif

{ 
    "to": "your_token_id",

    "data" : {
         "title" : "FCM Notification Title ",
         "subtext" : "FCM Notification Sub Title",
         "type" : "999",
         "priority" : "high"
    }
}
Android客户端 要处理Android接收器中接收到的有效负载,请查看官方指南

检查文档


你说的“不起作用”到底是什么意思?代码正在执行吗?如果是这样,你怎么知道?实际上onMessageReceived被称为onMessageReceived。因此,你已经验证了
onMessageReceived()
中的每一行都在执行?事实上,我已经解决了这个问题,我会很快将它发布到这里,谢谢你。
/* The class extends FirebaseMessagingService() */   
 override fun onMessageReceived(remoteMessage: RemoteMessage) {

        Log.d(TAG, "From: ${remoteMessage.from}")

        // Check if message contains a data payload.
        remoteMessage.data.isNotEmpty().let {
            Log.d(TAG, "Message data payload: " + remoteMessage.data)

        if (/* Check if data needs to be processed by long running job */ true) {
            // For long-running tasks (10 seconds or more) use WorkManager.
            scheduleJob()
        } else {
            // Handle message within 10 seconds
            handleNow()
        }
    }

    // Check if message contains a notification payload.
    remoteMessage.notification?.let {
        Log.d(TAG, "Message Notification Body: ${it.body}")
    }

    // 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.
}