Android-Firebase通知在应用程序位于后台时不会打开目标活动,但在前台正常工作

Android-Firebase通知在应用程序位于后台时不会打开目标活动,但在前台正常工作,android,firebase,notifications,firebase-cloud-messaging,Android,Firebase,Notifications,Firebase Cloud Messaging,我已经试过了stackoverflow的一些解决方案。但在我的情况下不能正常工作。 下面是我的MyFirebaseMessagingService类,它扩展了FirebaseMessagingService public class MyFirebaseMessagingService extends FirebaseMessagingService { String url , title , body ; Map<String,String> data =

我已经试过了stackoverflow的一些解决方案。但在我的情况下不能正常工作。 下面是我的
MyFirebaseMessagingService
类,它扩展了
FirebaseMessagingService

public class MyFirebaseMessagingService   extends FirebaseMessagingService {

    String url , title , body ;

    Map<String,String> data = new HashMap<>();

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {


        title = remoteMessage.getNotification().getTitle();
        body = remoteMessage.getNotification().getBody();

        if(remoteMessage.getData().size()>0){
            Log.d("DATA", remoteMessage.getData().toString());
            try{
                data = remoteMessage.getData();
                url = data.get("url");
            }
            catch (Exception e){
                Log.e("Error", e.toString());
            }

            showNotification(url , title , body);
        }






    }

    private void showNotification(String url, String title, String body) {
        Intent intent = new Intent(this, NotificationViewer.class);
        intent.putExtra("URL",url);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) ;
        notificationBuilder.setContentTitle(title);
        notificationBuilder.setContentText(body);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon(R.drawable.logo);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());

    }
}

我试过一些解决办法,但在我的情况下不起作用。当应用程序在前台但在后台不工作时,一切正常当用户单击通知时,我想打开一个特定的活动名称
NotificationViewer.Java
activity。但当我的应用程序在后台时,它会一直打开我的启动器活动。

您需要在通知请求中的json负载中设置单击操作
public class MyFirebaseMessagingService   extends FirebaseMessagingService {

    String url , title , body ;

    Map<String,String> data = new HashMap<>();

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {


        title = remoteMessage.getNotification().getTitle();
        body = remoteMessage.getNotification().getBody();

        if(remoteMessage.getData().size()>0){
            Log.d("DATA", remoteMessage.getData().toString());
            try{
                data = remoteMessage.getData();
                url = data.get("url");
            }
            catch (Exception e){
                Log.e("Error", e.toString());
            }

            showNotification(url , title , body);
        }






    }

    private void showNotification(String url, String title, String body) {
        Intent intent = new Intent(this, NotificationViewer.class);
        intent.putExtra("URL",url);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) ;
        notificationBuilder.setContentTitle(title);
        notificationBuilder.setContentText(body);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon(R.drawable.logo);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());

    }
}
'单击_action'=>'YourTAG'并在清单活动中应用相同的标记

<activity
      android:name=".activity.NotificationViewer">
      <intent-filter>
             <action android:name="YourTAG" />  
             <category android:name="android.intent.category.DEFAULT"/>              
      </intent-filter>
</activity>

您很可能使用Firebase通知消息,而不是数据消息

如果是这种情况,使用数据FCM消息应该可以帮到你

从中可以看出

当您希望FCM代表您的客户端应用程序处理显示通知时,请使用通知消息。要处理客户端应用程序上的消息时,请使用数据消息

通知消息

当应用程序位于前台时,它会接收有效负载,但在后台时,通知消息会传递到通知托盘

数据消息: 无论应用程序是在前台还是在后台,这些信息都会被发送到应用程序


注意:FCM数据消息本身不会将您的应用程序置于前台。您需要额外的代码,以便在收到数据信息时执行此操作。

谢谢您的评论。应该像下面这样绑定:
“数据”:{“url”:https://www.google.com“,”名称“:”Piash“,”单击操作“:”标记“}
您需要在通知对象中添加标记,例如:“单击操作“:”您的标记”从服务器端,我编辑我的AndroidManifest.xml,如下所示:
并添加“数据”:{“单击操作”:“操作标记”}`但仍然不工作,因为您正在添加内部数据obejct,您需要添加内部通知对象…感谢您的解决方案。我在通知对象中添加了它。现在它工作得很好。感谢这是因为当应用程序处于后台时,默认情况下通知由系统管理,FirebaseMessagingService从不被调用:请看我的答案:()