Android前台模式下第一次不显示推送通知的问题
创始人
2024-10-09 15:02:24
0

此问题可能是由于通知渠道未正确设置导致的。在创建通知之前,请使用以下代码创建通知渠道:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    // 设置通知渠道的ID、名称和描述
    String channelId = "my_channel_id";
    CharSequence channelName = "My Channel";
    String channelDescription = "Channel Description";
    int importance = NotificationManager.IMPORTANCE_HIGH;
    // 创建通知渠道
    NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
    channel.setDescription(channelDescription);
    // 注册通知渠道
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
}

然后,在发送通知时,请确保已使用正确的通知渠道 ID:

String channelId = "my_channel_id";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
    .setSmallIcon(R.drawable.ic_notification)
    .setContentTitle("My notification")
    .setContentText("Hello World!")
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, builder.build());

相关内容

热门资讯

普及透视!德州圈脚本!切实一直... 普及透视!德州圈脚本!切实一直都是有辅助神器(真是有挂)-哔哩哔哩一、德州圈脚本可以开透视的定义与意...
曝光透视!pokermaste... 曝光透视!pokermaster辅助器,微信小程序辅助多少钱,指南教程(真是有挂)-哔哩哔哩1、首先...
开挂透视!pokemmo脚本辅... 开挂透视!pokemmo脚本辅助器!真是存在有辅助教程(真是有挂)-哔哩哔哩1)pokemmo脚本辅...
专业透视!wepoker辅助真... 专业透视!wepoker辅助真的假的,微信小程序微乐辅助器贴吧,手段教程(讲解有挂)-哔哩哔哩1)w...
揭露透视!佛手在线大菠萝技巧!... 揭露透视!佛手在线大菠萝技巧!确实真的是有辅助攻略(有挂教程)-哔哩哔哩1、上手简单,内置详细流程视...
解密透视!如何下载wepoke... 解密透视!如何下载wepoker安装包,微乐广西小程序,教材教程(有挂透明挂)-哔哩哔哩1、超多福利...
解谜透视!xpoker辅助控制... 解谜透视!xpoker辅助控制!本来一直总是有辅助神器(确实有挂)-哔哩哔哩1、起透看视 xpoke...
详情透视!hhpkoer辅助挂... 详情透视!hhpkoer辅助挂是真的吗,微乐家乡官方免费下载安装,总结教程(有挂教程)-哔哩哔哩暗藏...
了解透视!哈糖大菠萝软件下载!... 您好,哈糖大菠萝软件下载这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多...
开挂透视!sohoo辅助,微乐... 您好,sohoo辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家...