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文件中声明了相关权限和服务。

相关内容

热门资讯

科普攻略!wepoker透视是... 科普攻略!wepoker透视是真的吗(辅助挂)一贯真的是有挂(果真有挂辅助软件)1、点击下载安装,脚...
揭秘攻略!wepoker代打辅... 揭秘攻略!wepoker代打辅助(辅助挂)总是是有挂(讲解有挂辅助软件)1、免费脚本咨询教程、免费脚...
玩家必看攻略!wepoker免... 玩家必看攻略!wepoker免费脚本弱密码(辅助挂)一直真的是有挂(有挂方法辅助脚本)1、任何透视是...
大家学习交流!hhpoker哪... 大家学习交流!hhpoker哪个俱乐部靠谱(辅助挂)都是是有挂(有挂教程辅助插件)1、许多玩家不知道...
每日必看推荐!来玩app 德州... 每日必看推荐!来玩app 德州 辅助(辅助挂)切实是有挂(存在有挂辅助插件)1、模拟器是什么优化,俱...
查到实测辅助!pokemmo手... 查到实测辅助!pokemmo手机脚本(辅助挂)确实真的是有挂(有挂秘籍辅助方法)1、用户打开应用后不...
技术分享!hhpoker破解工... 技术分享!hhpoker破解工具(辅助挂)好像是真的有挂(证实有挂辅助方法)是不是有人用挂微扑克wp...
重要通知!wepokerplu... 重要通知!wepokerplus外挂(辅助挂)好像是真的有挂(果真有挂辅助工具)脚本下载中分为三种模...
技术分享!wepoker有辅助... 技术分享!wepoker有辅助吗(辅助挂)总是存在有挂(有挂技巧辅助方法)1、首先打开辅助器下载最新...
一秒答解!agpoker辅助(... 一秒答解!agpoker辅助(辅助挂)确实真的有挂(的确有挂辅助攻略)1、进入游戏-大厅左侧-新手福...