服务器重置后无法通过firebase接收消息

Ron*_*erg 5 java service android firebase server

我有一个移动应用程序,通过firebase云消息传递从服务器获取通知.

当我启动移动应用程序并进行订阅时,移动应用程序收到了服务器的通知.

问题是当服务器停机并重新启动时.重置后,服务器正在向移动应用程序发送通知(使用与之前相同的过程),但移动应用程序不会收到任何内容.请记住,移动应用程序同时保持不变(已注册)(服务器非常快速地执行重置).

服务器的IP和端口保持不变......

以下是订阅正确频道的移动代码:

public class MainActivity extends AppCompatActivity  {

    private static final String TAG = "MainActivity";

    private static final String NEW_CONTACTS_TOPIC = "new_contacts";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Create channel to show notifications.
            String channelId  = getString(R.string.default_notification_channel_id);
            String channelName = getString(R.string.default_notification_channel_name);
            NotificationManager notificationManager =
                    getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(new NotificationChannel(channelId,
                    channelName, NotificationManager.IMPORTANCE_LOW));
        }

        if (getIntent().getExtras() != null) {
            for (String key : getIntent().getExtras().keySet()) {
                Object value = getIntent().getExtras().get(key);
                Log.d(TAG, "Key: " + key + " Value: " + value);
            }
        }

        Button subscribeButton = findViewById(R.id.subscribeButton);
        subscribeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FirebaseMessaging.getInstance().subscribeToTopic(NEW_CONTACTS_TOPIC);

                // Log and toast
                String msg = getString(R.string.msg_subscribed);
                Log.d(TAG, msg);
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });

        Button logTokenButton = findViewById(R.id.logTokenButton);
        logTokenButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Get token
                String token = FirebaseInstanceId.getInstance().getToken();

                // Log and toast
                String msg = getString(R.string.msg_token_fmt, token);
                Log.d(TAG, msg);
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            try {
                handleNow(remoteMessage);
            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            } catch (InterruptedException e) {
                Log.e(TAG, e.getMessage());
            }
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是我的服务器中发送通知的过程:

@Service
public class AndroidPushNotificationsService {

private static final String FIREBASE_SERVER_KEY = "XXXX";
private static final String FIREBASE_API_URL = "https://fcm.googleapis.com/fcm/send";

@Async
public CompletableFuture<String> send(HttpEntity<String> entity) {

    RestTemplate restTemplate = new RestTemplate();

    ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
    interceptors.add(new HeaderRequestInterceptor("Authorization", "key=" + FIREBASE_SERVER_KEY));
    interceptors.add(new HeaderRequestInterceptor("Content-Type", "application/json"));
    restTemplate.setInterceptors(interceptors);

    String firebaseResponse = restTemplate.postForObject(FIREBASE_API_URL, entity, String.class);

    return CompletableFuture.completedFuture(firebaseResponse);
}
Run Code Online (Sandbox Code Playgroud)

重要提示:当服务器启动时,服务器和移动应用程序之间的通信可以长时间工作......

我该如何解决这个问题?

小智 1

在某些情况下,FCM 可能不会传递消息。当消息太多 (>100) 时会发生这种情况

在 Firebase 文档中说: 当 FCM 服务器删除待处理消息时调用onDeletedMessages() 。

这可能是由于:

  1. FCM 服务器上存储的消息过多。当设备离线时应用程序的服务器向 FCM 服务器发送一堆不可折叠的消息时,可能会发生这种情况。
  2. 该设备已经很长时间没有连接,并且应用程序服务器最近(在过去 4 周内)向该设备上的应用程序发送了一条消息。