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

相关内容

热门资讯

最新技巧“sohoo开挂辅助”... sohoo开挂辅助是一款专注玩家量身打造的游戏记牌类型软件,在sohoo开挂辅助这款游戏中我们可以记...
分享一款“盛世辅助”开挂(透视... 大家好,今天小编来为大家解答盛世辅助这个问题咨询软件客服可以免费测试直接加微信(136704302)...
透视软件“闽游十三水辅助”开挂... 透视软件“闽游十三水辅助”开挂(透视)辅助脚本揭秘攻略(有挂技术) 【无需打开直接搜索加薇13670...
专业讨论“开心十三张脚本”开挂... 专业讨论“开心十三张脚本”开挂(透视)辅助插件必备教程(有挂细节)《详细加薇136704302咨询》...
查到实测“悠悠互娱辅助”开挂(... 查到实测“悠悠互娱辅助”开挂(透视)辅助安装爆料教程(果真有挂)这是一款可以让一直输的玩家,快速成为...
揭秘真相“蜜瓜大厅小程序辅助”... 您好:这款蜜瓜大厅小程序辅助游戏是可以开挂的,确实是有挂的,很多玩家在这款蜜瓜大厅小程序辅助游戏中打...
透视实锤“雀神广东麻将透视视频... 透视实锤“雀神广东麻将透视视频”开挂(透视)辅助平台透明挂教程(有挂猫腻) 【无需打开直接搜索加薇1...
分享实测“吉祥填大坑脚本”开挂... 分享实测“吉祥填大坑脚本”开挂(透视)辅助透视普及教程(新版有挂)>>您好:软件加薇13670430...
重大推荐“传送屋app辅助脚本... 重大推荐“传送屋app辅助脚本下载”开挂(透视)辅助软件可靠技巧(详细教程)传送屋app辅助脚本下载...
重大来袭“wepoker辅助”... 重大来袭“wepoker辅助”开挂(透视)辅助插件黑科技教程(有挂神器) 【无需打开直接搜索加薇13...