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

相关内容

热门资讯

透视系统!德州透视插件,hhp... 透视系统!德州透视插件,hhpoker有透视挂“解迷开挂透视挂辅助神器”1、hhpoker有透视挂系...
透视有挂!wepoker透视脚... 透视有挂!wepoker透视脚本,拱趴大菠萝可以开挂“揭露开挂透视挂辅助方法”进入游戏-大厅左侧-新...
透视存在!wepoker辅助器... 透视存在!wepoker辅助器有哪些功能,hhpoker哪个俱乐部靠谱“关于开挂透视挂辅助软件”1、...
透视辅助!竞技联盟透视插件,h... 透视辅助!竞技联盟透视插件,hhpoker有透视挂“了解开挂透视挂辅助神器”1、每一步都需要思考,不...
透视计算!xpoker透视辅助... 透视计算!xpoker透视辅助,wepoker脚本下载“科普开挂透视挂辅助插件”进入游戏-大厅左侧-...
透视游戏!wepoker有透视... 透视游戏!wepoker有透视,wepoker手机助手“详情开挂透视挂辅助神器”1、wepoker手...
透视肯定!竞技联盟破解版最新版... 透视肯定!竞技联盟破解版最新版,we-poker辅助器“必备开挂透视挂辅助神器”1、操作简单,无需注...
透视透视!wepoker到底有... 透视透视!wepoker到底有透视,wepoker可以免费玩“必备开挂透视挂辅助软件”wepoker...
透视美元局!wejoker辅助... 透视美元局!wejoker辅助机器人,菠萝德普辅助器免费版在哪里“科普开挂透视挂辅助教程”1、游戏颠...
透视安装!wepokerplu... 透视安装!wepokerplus透视挂,wepoker辅助分析器“解密开挂透视挂辅助攻略”1、wep...