简体   繁体   中英

Notification Doesn't work in the foreground

My notification work if the app is in the background but it doesn't work in the foreground

this is my MyFirebaseMessagingService class:

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());



    }

}

Please, how can i resolve this problem?

FCM has different behaviours for app status (foreground and background / killed). You should handle this by the payload you sent from server, according to your use case.

The msg sent from server has to be sent in either "notification" or "data" format, from dashboard or server side api. Note: From firebase dashobard you can only send "notification" body and not data. In such cases, FCM will directly display the notif without giving a callback to your app.

Server side Below are sample formats :

Notification Type Format Note : Android System will by default display the notification in the notification tray and you don't need to display it.

 { 
    "to": "your_token_id",
     "notification" : {
             "title" : "FCM Notification title!",
             "body" : "FCM Notification subtext!",
             "content_available" : true,
             "priority" : "high"
     }
}

Data Format (For receiving callback in app, in foreground and background) Note : You have to handle callback and display notif on your own.

{ 
    "to": "your_token_id",

    "data" : {
         "title" : "FCM Notification Title ",
         "subtext" : "FCM Notification Sub Title",
         "type" : "999",
         "priority" : "high"
    }
}

Android Client To handle the payload received in your Android receiver, checl the official guide here

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

Check the documentation here

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM