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

相关内容

热门资讯

WPK链接!红龙PoKer脚本... WPK链接!红龙PoKer脚本开挂ai,wpk辅助器,真是是真的有ai开挂方式wpk辅助器辅助器是一...
WPK脚本!WePoKer攻略... WPK脚本!WePoKer攻略透视挂ai,智星德州辅助译码插件靠谱吗,真是是真的有ai透视挂技法1、...
WPK透视版!AApoker攻... WPK透视版!AApoker攻略辅助器ai,AApoker中牌率,果然有ai辅助器讲义1、首先打开A...
WPK四人!智星菠萝德州技巧开... WPK四人!智星菠萝德州技巧开挂ai,aapoker ai插件,好像真的是有ai开挂绝活1、这是跨平...
WPK安卓!wpk软件透视挂a... WPK安卓!wpk软件透视挂ai,wpk系统是否存在作弊行为,确实是有ai透视挂指南1、该软件可以轻...
WPK工具!aapoker脚本... WPK工具!aapoker脚本透视挂ai,拱趴大菠萝十三水作弊,切实存在有ai透视挂窍要1)拱趴大菠...
WPK私人局!德州扑克脚本透视... WPK私人局!德州扑克脚本透视ai,wpk透视是真的吗,一贯是真的有ai透视秘籍1、玩家可以在wpk...
WPK辅助!德州扑克脚本透视a... WPK辅助!德州扑克脚本透视ai,AApoker透视,一直真的是有ai透视策略1、AApoker透视...
WPK私人局!WePoKera... WPK私人局!WePoKerapp开挂ai,德普之星透视免费,总是有ai开挂方针1、每一步都需要思考...
WPK透视版!菠萝智星德州软件... WPK透视版!菠萝智星德州软件透视挂ai,aapoker辅助器怎么用,一直有ai透视挂举措1、许多玩...