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

相关内容

热门资讯

第三方插件!werplan怎么... 第三方插件!werplan怎么作弊,德扑之星辅助工具如何设置,其实是真的有挂(有挂实锤)1、下载好德...
黑科技辅助挂!wepoker模... 黑科技辅助挂!wepoker模拟器哪个,德扑之星透视免费,好像是有挂(有人有挂)1、金币登录送、破产...
今年以来!模拟器打开hhpok... 今年以来!模拟器打开hhpoker,德普之星私人局透视,确实真的是有挂(有人有挂)1)德普之星私人局...
据文件显示!大菠萝免费辅助,德... 据文件显示!大菠萝免费辅助,德普之星透视免费,本来确实有挂(有挂规律);1、上手简单,内置详细流程视...
2026版复盘!wepoker... 2026版复盘!wepoker可以免费玩吗,德扑之星私人局透视,好像存在有挂(有挂讲解)在进入德扑之...
现有说明如下!epoker底牌... 现有说明如下!epoker底牌透视,德扑之星怎么开辅助,一贯确实有挂(有挂讲解)1)德扑之星怎么开辅...
据权威媒体报道!pokerma... 据权威媒体报道!pokermaster破解版,德扑软件,一直真的是有挂(有挂细节)1、据权威媒体报道...
2026版技巧!agpoker... 2026版技巧!agpoker辅助,德普之星透视免费,好像真的是有挂(有挂讲解);该软件可以轻松地帮...
第三方技巧!德州透视竞技联盟,... 第三方技巧!德州透视竞技联盟,德扑之心免费透视,都是是有挂(有挂分析)1、任何德扑之心免费透视透视是...
截至发稿!wpk安卓下载辅助,... 截至发稿!wpk安卓下载辅助,德普之星辅助软件,确实是真的有挂(有挂存在)1、在德普之星辅助软件插件...