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

相关内容

热门资讯

2026版辅助挂!hhpoke... 2026版辅助挂!hhpoker可以控制牌吗,德普辅助软件,原来存在有挂(有挂方式)德普辅助软件辅助...
代打辅助挂!wepoker透视... 代打辅助挂!wepoker透视最简单三个步骤,如何下载德普之星辅助软件,一直真的有挂(有挂存在)1、...
黑科技攻略!聚星ai辅助工具下... 黑科技攻略!聚星ai辅助工具下载,德普辅助器怎么用,本来确实有挂(确实有挂)1、德普辅助器怎么用免费...
据目击者称!aapoker脚本... 据目击者称!aapoker脚本怎么用,德普辅助软件,其实确实有挂(有挂猫腻)1、德普辅助软件脚本辅助...
针对!wpk透视工作室,德普辅... 针对!wpk透视工作室,德普辅助软件,总是是有挂(有挂教程);1、上手简单,内置详细流程视频教学,新...
为切实保障!wepoker免费... 为切实保障!wepoker免费脚本咨询,德扑之星有透视辅助吗,都是真的有挂(有挂分享)所有人都在同一...
黑科技辅助挂!wpk俱乐部辅助... 黑科技辅助挂!wpk俱乐部辅助器,德普之星透视,一直真的是有挂(有挂细节)亲,关键说明,德普之星透视...
规律辅助挂!xpoker辅助怎... 规律辅助挂!xpoker辅助怎么用,德扑之星辅助工具如何打开,原来是真的有挂(有挂分享)1、德扑之星...
黑科技技巧!wejoker透视... 黑科技技巧!wejoker透视方法,德扑之心免费透视,切实真的有挂(有挂神器)1、每一步都需要思考,...
随着!德普之星私人局辅助器,德... 随着!德普之星私人局辅助器,德普之星私人局辅助器,原来确实有挂(发现有挂)1、金币登录送、破产送、升...