要解决 Android 手机推送通知延迟的问题,需要进行以下操作:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, builder.build());
使用 Firebase Cloud Messaging(FCM)推送通知:
//发送通知消息
FirebaseMessaging.getInstance().send(new RemoteMessage.Builder()
.setMessageId(Integer.toString(msgId))
.addData("title", title)
.addData("message", message)
.setToken(token)
.build());
//处理接收到的通知消息
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//解析通知消息
String title = remoteMessage.getData().get("title");
String message = remoteMessage.getData().get("message");
//显示通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable