Android推送在几秒后消失。
创始人
2024-08-21 21:00:44
0

以下是一个在Android中实现推送消息在几秒后消失的代码示例:

  1. 创建一个新的类NotificationUtils
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.NotificationCompat;

public class NotificationUtils {
    private static final int NOTIFICATION_ID = 1;
    private static final int DELAY_MILLIS = 3000; // 3秒

    public static void showNotification(Context context, String title, String message) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channel_id")
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true);

        Notification notification = builder.build();

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, notification);

        // 注册一个延迟的任务,在指定时间后取消通知
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                notificationManager.cancel(NOTIFICATION_ID);
            }
        }, DELAY_MILLIS);
    }
}
  1. 在你的推送消息接收到的地方调用NotificationUtils.showNotification()方法来显示通知,例如在onMessageReceived()方法中:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // 解析推送消息
    String title = remoteMessage.getData().get("title");
    String message = remoteMessage.getData().get("message");

    // 显示通知
    NotificationUtils.showNotification(getApplicationContext(), title, message);
}

请确保你已经创建了一个合适的通知渠道(channel_id)并在AndroidManifest.xml文件中声明了相关权限和服务。

相关内容

热门资讯

开挂辅助!微乐广西小程序插件,... 开挂辅助!微乐广西小程序插件,wpk是真的还是假的(透视)开挂辅助下载(有挂存在);微乐广西小程序插...
辅助开挂!互游辅助518,拱趴... 辅助开挂!互游辅助518,拱趴大菠萝辅助神器(透视)开挂辅助软件(有挂功能);亲,有的,ai轻松简单...
辅助开挂!青鸟辅助安卓,约局吧... 辅助开挂!青鸟辅助安卓,约局吧辅助器(透视)开挂辅助脚本(有挂工具);青鸟辅助安卓是一项青鸟辅助安卓...
开挂辅助!科乐辅助工作室,we... 开挂辅助!科乐辅助工作室,wepoker私人局开挂视频(透视)开挂辅助工具(有挂神器);科乐辅助工作...
开挂辅助!天天互娱app辅助,... 开挂辅助!天天互娱app辅助,拱趴游戏破解器(透视)开挂辅助插件(存在有挂);详细天天互娱app辅助...
开挂辅助!大众互娱脚本,wep... 开挂辅助!大众互娱脚本,wepoker是不是有人用挂(透视)开挂辅助平台(有挂方略);一、大众互娱脚...
开挂辅助!蜀山手游全自动辅助,... 开挂辅助!蜀山手游全自动辅助,wepoker怎么提高运气(透视)开挂辅助脚本(今日头条);亲真的是有...
辅助开挂!龙岩闲游安卓脚本,w... 辅助开挂!龙岩闲游安卓脚本,wepoker轻量版有透视吗(透视)开挂辅助神器(有挂方式);大家肯定在...
开挂辅助!哥哥打大a游戏攻略,... 开挂辅助!哥哥打大a游戏攻略,wepoker私人局有透视吗(透视)开挂辅助插件(果真有挂);哥哥打大...
开挂辅助!h5大厅反杀,德扑H... 【福星临门,好运相随】;开挂辅助!h5大厅反杀,德扑HHpoker有挂吗(透视)开挂辅助神器(有挂教...