Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java FCM onMessageReceived在应用程序被终止时不触发(Android Oreo)_Java_Android - Fatal编程技术网

Java FCM onMessageReceived在应用程序被终止时不触发(Android Oreo)

Java FCM onMessageReceived在应用程序被终止时不触发(Android Oreo),java,android,Java,Android,我正在使用一个应用程序,当发送FCM消息时,它需要打开前台服务。我的代码是: FirebaseService.class: import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import andr

我正在使用一个应用程序,当发送FCM消息时,它需要打开前台服务。我的代码是: FirebaseService.class:

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.io.IOException;

public class FirebaseService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d("From",remoteMessage.getFrom());
        Intent intt = new Intent(this,foreground.class);
        startActivity(intt);
    }

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        String channelId = "TT";
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_launcher_foreground)
                        .setContentTitle("TR")
                        .setContentText(messageBody)
                        .setAutoCancel(true);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
        Log.d("TEST","received");
        Server server = new Server();
        try {
            if(server.connect())
                Log.d("TEST","Connected");
            else
                Log.d("TEST","NOT Connected");

        } catch (Exception e) {
            e.printStackTrace();
        }
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

}

但是,当我发送meesage时,它会向我显示另一个通知(不是我在函数中创建的通知),其中包含我在发送meesage时编写的消息正文和标题。(在函数中,我不会从收到的消息中发送标题和正文,而只是随机添加一些文本)。我的前台类没有被解雇。

我不确定,但我遇到了几乎相同的问题,并要求用户批准

ACTION_MANAGE_OVERLAY_PERMISSION
解决了我的问题。在那之后,前台类被解雇了


你说的是android 10+的什么。我在奥利奥工作,所以没有意义。无论如何,谢谢你的回答。这在奥利奥是真的,你不需要这个许可。您确定它适用于服务的上下文吗?在这种情况下,你可以考虑看看这个: