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透视是真... 更值得关注的是!wpk透视是真的吗,pokemmo辅助工具,学习教程(有挂教学)1、起透看视 wpk...
此事引发网友热议!wepoke... 此事引发网友热议!wepoker辅助器下载,hhpoker是内部控制吗,烘培教程(真的有挂)hhpo...
代打辅助挂!epoker有透视... 代打辅助挂!epoker有透视吗,约局吧德州有挂吗,操作教程(有挂教学)1、全新机制【约局吧德州有挂...
于此同时!智星菠萝有挂吗,we... 于此同时!智星菠萝有挂吗,wepoker数据分析,绝活教程(有挂教程)1、实时智星菠萝有挂吗透视辅助...
黑科技攻略!werplan脚本... 黑科技攻略!werplan脚本,pokemmo手机脚本,法门教程(有挂总结)1、首先打开pokemm...
受玩家影响!德普之星私人局辅助... 受玩家影响!德普之星私人局辅助免费,wepoker透视最简单三个步骤,妙计教程(发现有挂)该软件可以...
2026版方法!来玩app破解... 2026版方法!来玩app破解,模拟器打开hhpoker,方针教程(有挂透明挂)1、模拟器打开hhp...
值得注意的是!wepoker透... 值得注意的是!wepoker透视脚本是什么,wepoker永久免费脚本,项目教程(今日头条)亲,关键...
代打辅助挂!德普之星私人局透视... 代打辅助挂!德普之星私人局透视,佛手大菠萝13道挂哪里,积累教程(的确有挂)1、打开软件启动之后找到...
连日来!wepoker透视有用... 连日来!wepoker透视有用吗,cloudpoker怎么开挂,总结教程(有挂秘诀)wepoker透...