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

相关内容

热门资讯

原来有开挂!三哥玩摆头挂,福建... 原来有开挂!三哥玩摆头挂,福建天天开心辅助器真的假的(推荐一款开挂辅助安装);无需打开直接搜索加(薇...
实测辅助!新海贝之城辅助器,福... 实测辅助!新海贝之城辅助器,福建老友破解(透视美元局开挂辅助安装)海贝之城辅助器ai黑科技系统规律教...
教会开挂!卡农辅助,来物局怎么... 教会开挂!卡农辅助,来物局怎么下载(推荐几款新版开挂辅助软件)来物局怎么下载ai黑科技系统规律教程开...
详细开挂!新全游辅助器,桂林字... 详细开挂!新全游辅助器,桂林字牌辅助(信息共享开挂辅助工具)【无需打开直接搜索加薇136704302...
分析开挂!财神十三章张助安装包... 分析开挂!财神十三章张助安装包,闲逸碰胡辅助器(教学盘点开挂辅助下载)【无需打开直接搜索加薇1367...
分析开挂!随意玩5元流量包,智... 您好:随意玩5元流量包这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...
正版开挂!新天天贵阳麻将辅助工... 正版开挂!新天天贵阳麻将辅助工具,桂麻圈辅助(盘点一款开挂辅助神器)>>您好:软件加薇1367043...
细节辅助!新蜜瓜大厅控制器,随... 您好:新蜜瓜大厅控制器这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...
原来有辅助!雀友会辅助潮汕麻将... 原来有辅助!雀友会辅助潮汕麻将脚本,皮皮游戏辅助平台(重大通报开挂辅助安装);打开点击测试直接进入微...
正品开挂!悟空大厅辅助,多多科... 正品开挂!悟空大厅辅助,多多科技手游辅助(透视数据开挂辅助脚本);一、多多科技手游辅助AI软件牌型概...