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

相关内容

热门资讯

科技分享!uupoker有透视... 科技分享!uupoker有透视吗,aapoker透视脚本入口,好像存在有挂(有挂存在)科技分享!uu...
一分钟揭秘!微扑克游戏辅助器,... 一分钟揭秘!微扑克游戏辅助器,德普之星辅助器app,切实真的有挂(有挂总结)1、用户打开应用后不用登...
三分钟了解!wepoker有透... 三分钟了解!wepoker有透视功能吗,aapoker怎么控制牌,原来是有挂(真实有挂)1、辅助器安...
总算了解!pokemmo手机版... 总算了解!pokemmo手机版修改器,wepoker怎么挂飞机,其实真的是有挂(有挂秘诀)1、模拟器...
科普攻略!菠萝德州透视脚本,w... 科普攻略!菠萝德州透视脚本,wepoker破解游戏盒子,一贯有挂(有挂透明挂)1、首先打开辅助器下载...
分辨真假!xpoker辅助器,... 分辨真假!xpoker辅助器,aapoker透视插件,切实真的有挂(有挂规律)在进入软件靠谱后,参与...
终于清楚!约局吧德州真的存在透... 终于清楚!约局吧德州真的存在透视吗,aapoker真的假的,真是是真的有挂(有挂方针)该软件可以轻松...
一分钟揭秘!wepoker私人... 一分钟揭秘!wepoker私人局透视插件,德普之星透视软件免费入口官网,竟然真的有挂(有挂秘籍)1、...
玩家必看秘籍!wpk辅助哪里买... 玩家必看秘籍!wpk辅助哪里买,竞技联盟破解版最新版,一直是真的有挂(真的有挂)破解侠是真的助透视。...
实测揭晓!wepoker轻量版... 实测揭晓!wepoker轻量版书签,购买的wpk辅助在哪里下载,一贯真的有挂(有挂透明挂)1、首先打...