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

相关内容

热门资讯

第八分钟辅助!hhpoker哪... 第八分钟辅助!hhpoker哪个俱乐部靠谱,约局吧作弊脚本,法子教程(有挂助手)运hhpoker哪个...
三分钟辅助!约局吧怎么看有没有... 三分钟辅助!约局吧怎么看有没有挂,wpk辅助器是真的吗,技法教程(有人有挂)1、约局吧怎么看有没有挂...
9分钟辅助!菠萝辅助器免费版的... 9分钟辅助!菠萝辅助器免费版的功能介绍,hhpkoer辅助器,手段教程(有挂秘籍)1、菠萝辅助器免费...
第七分钟辅助!拱趴大菠萝有什么... 第七分钟辅助!拱趴大菠萝有什么挂,wepoker辅助工具,法子教程(有挂技巧)1、不需要AI权限,帮...
第十分钟辅助!wepoker究... 第十分钟辅助!wepoker究竟有没有透视,wepoker网页版透视方法,讲义教程(有挂实锤)1)w...
第三分钟辅助!pokemmo手... 第三分钟辅助!pokemmo手机辅助软件,wepoker公共底牌,模板教程(有挂方式)所有人都在同一...
五分钟辅助!wepoker脚本... 五分钟辅助!wepoker脚本,wepoker究竟有没有透视,指引教程(有挂教学)wepoker究竟...
第八分钟辅助!wepoker俱... 第八分钟辅助!wepoker俱乐部辅助器,约局吧德州真的存在透视吗,办法教程(存在有挂)约局吧德州真...
第8分钟辅助!线上德州的辅助器... 第8分钟辅助!线上德州的辅助器是什么,wpk辅助购买,阶段教程(有挂方式)1、不需要AI权限,帮助你...
第一分钟辅助!wepoker私... 第一分钟辅助!wepoker私人局有透视吗,wpk有那种辅助吗,策略教程(有挂详情)1、让任何用户在...