收到重复的 FCM 通知

Receiving Duplicate FCM Notifications

我已经实现了接收 FCM 通知的代码,我已经设置了代码并且一切正常,但问题是通知重复,我检查了 post 请求,但它没有导致任何重复但 onMessageReceived 被调用两次,我无法弄清楚原因,我已经检查了一些关于我的问题的其他主题,我必须检查我是否有任何触发 GCM 和 FCM 的东西,但一切都是好的。


 public class NotificationService extends FirebaseMessagingService {

    private int num;
    private RequestQueue requestQueue;
   
    private String serverUrl = "https://fcm.googleapis.com/fcm/send";
    private String authKey = "key=AIzaSyAwrrIexxxxxxxxxxxxxxxxxxx";

     ///i'm sending data to server in oncreate method
    @Override
    public void onCreate() {
        super.onCreate();

        PostDataQll();
    }

     ///this is where i received message back from server
    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        Map<String, String> map = remoteMessage.getData();
        String title = map.get("title");
        String body = map.get("body");

        if (!Objects.requireNonNull(body).equals("")) {
            shownotification(body, title);
        }

    }

    //////show notification 
    void shownotification(String mytime, String awayteam) {

        num = (int) System.currentTimeMillis();

        Notification notification = new NotificationCompat.Builder(getApplicationContext(), 
                 notificationhelper.channelid)
                .setContentText("Now : " + awayteam)
                .setContentTitle(mytime)
                .setChannelId(notificationhelper.channelid)
                .setSmallIcon(R.drawable.ic_notifications)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .build();

        for (int i = 0; i < 1; i++) {
            SystemClock.sleep(3000);
            NotificationManagerCompat.from(this).notify(num, notification);
        }


    }
    /////posting data to the fcm server
    void post(String url, String json) {
        Log.d("TODO", "posted data" + " " + "Yes posted");
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");

        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(JSON, json);
        okhttp3.Request request = new okhttp3.Request.Builder()
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", authKey)
                .url(url)
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });

    }
     ////in this method , the user subscribes to the topic 
     //// this is just quickie sample of the data i'm sending to server 
     private void PostDataQll() {


        FirebaseMessaging.getInstance().subscribeToTopic("spanishsport");

        JSONObject jsonObject = new JSONObject();
        JSONObject notification = new JSONObject();

        try {
            jsonObject.put("to", "/topics/spanishsport");
            notification.put("title", "France" + " vs " + "Italy");
            notification.put("body", "20'");
            jsonObject.put("data", notification);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        post("https://fcm.googleapis.com/fcm/send", jsonObject.toString());


    }
       <service
            android:name=".notificationui.NotificationService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

我打电话给@L2_Paver 说,我必须 post 我的片段中的 json 对象。