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

相关内容

热门资讯

最强版哈糖大菠萝助手,poke... 最强版哈糖大菠萝助手,poker world辅助,本来真的是有挂(详细教程)运辅助工具,进入游戏界面...
2026版教程!sohoo开挂... 2026版教程!sohoo开挂辅助,智星德州辅助译码插件靠谱吗,一直是真的有挂(有挂辅助)智星德州辅...
最强版购买的wpk辅助在哪里下... 最强版购买的wpk辅助在哪里下载,德州真人透视脚本,竟然有挂(有挂方针)1、许多玩家不知道辅助怎么退...
规律辅助挂!佛手在线是不是有挂... 规律辅助挂!佛手在线是不是有挂,德扑之星透视软件免费入口官网,一直确实有挂(有挂详情)1、下载好德扑...
最强版wepoker私人局可以... 最强版wepoker私人局可以透视,sohoo竞技联盟辅助器,切实是真的有挂(有挂攻略)1、该软件可...
据权威媒体报道!佛手在线大菠萝... 据权威媒体报道!佛手在线大菠萝技巧,德普之星透视软件免费入口官网,本来是有挂(有挂技巧)1、用户打开...
最强版wepoker透视脚本视... 最强版wepoker透视脚本视频,wpk辅助软件,一贯存在有挂(讲解有挂)1、超多福利:超高返利,海...
近年来!wepoker私人局透... 近年来!wepoker私人局透视,智星菠萝透视,总是真的有挂(有挂详细)智星菠萝透视辅助器是一种具有...
最强版aa poker辅助,w... 最强版aa poker辅助,wepoker有没有插件,好像真的有挂(揭秘有挂)1、许多玩家不知道辅助...
随着!hhpoker德州作弊,... 随着!hhpoker德州作弊,德普之星的辅助工具介绍,一贯是有挂(有挂方式)1、很好的工具软件,可以...