Android前台服务发送通知时发送的是通用通知而非自定义通知
创始人
2024-10-09 15:02:02
0

可以使用不同的通知ID来区分前台服务通知和其他通知。示例代码如下:

private static final int NOTIFICATION_ID_SERVICE = 1;
private static final int NOTIFICATION_ID_ACTION = 2;

private void startForeground() {
    // Create a notification channel for the service notification.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel serviceChannel = new NotificationChannel(
                NOTIFICATION_CHANNEL_ID,
                "Foreground Service Channel",
                NotificationManager.IMPORTANCE_DEFAULT
        );
        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(serviceChannel);
    }

    // Create the notification for the foreground service.
    Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
            .setContentTitle("My Foreground Service")
            .setContentText("Running...")
            .setSmallIcon(R.drawable.ic_notification)
            .build();

    // Start the service in the foreground with the notification.
    startForeground(NOTIFICATION_ID_SERVICE, notification);
}

private void showActionNotification() {
    // Create the notification for an action.
    Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
            .setContentTitle("Action Notification")
            .setContentText("Something happened...")
            .setSmallIcon(R.drawable.ic_notification)
            .build();

    // Show the notification.
    NotificationManagerCompat manager = NotificationManagerCompat.from(this);
    manager.notify(NOTIFICATION_ID_ACTION, notification);
}

在这个示例中,我们创建了两个不同的通知ID:NOTIFICATION_ID_SERVICENOTIFICATION_ID_ACTION,其中NOTIFICATION_ID_SERVICE用于启动前台服务时显示的通知,而NOTIFICATION_ID_ACTION用于其他通知。

当我们需要显示前台服务通知时,我们调用startForeground()方法,并传递NOTIFICATION_ID_SERVICE和前台服务通知的内容。

当我们需要显示其他通知时,我们调用showActionNotification()方法,并传递NOTIFICATION_ID_ACTION和其他通知的内容。

使用不同的通知ID可以确保前台服务通知和其他通知

相关内容

热门资讯

每日必备!奇迹手游辅助,途游破... 每日必备!奇迹手游辅助,途游破解版无限钻石(都是有挂)1、途游破解版无限钻石辅助软件下载优化,途游破...
六分钟了解!闲聚鱼虾蟹软件脚本... 六分钟了解!闲聚鱼虾蟹软件脚本,pokemmo辅助器脚本下载,介绍教程(有挂神器);六分钟了解!闲聚...
每日必看推荐!哈局八张有没有透... 每日必看推荐!哈局八张有没有透视挂,雀友会鱼虾蟹下载官网辅助器(其实真的是有挂)1、雀友会鱼虾蟹下载...
总算清楚!同城游抓狗腿辅助,约... 总算清楚!同城游抓狗腿辅助,约局吧辅助器,必胜教程(真的有挂);同城游抓狗腿辅助软件透明挂是一个全新...
重大发现!爱玩联盟辅助软件视频... 重大发现!爱玩联盟辅助软件视频讲解,创思维激k透视源码(果然存在有挂)1)爱玩联盟辅助软件视频讲解有...
免费测试版!情怀宜春辅助,we... 免费测试版!情怀宜春辅助,werplan外卦神器,黑科技教程(有挂分享);情怀宜春辅助是一项情怀宜春...
技术分享!广东雀神智能插件辅助... 技术分享!广东雀神智能插件辅助脚本,吉安中至小程序辅助(果然存在有挂)1、在广东雀神智能插件辅助脚本...
科技分享!途游休闲铺破解版,w... 科技分享!途游休闲铺破解版,wepoker透视脚本安卓,靠谱教程(有挂秘籍);1、许多玩家不知道途游...
玩家必备教程!杭州都莱辅助软件... 玩家必备教程!杭州都莱辅助软件下载,新超凡软甲辅助(原来真的是有挂)1、首先打开杭州都莱辅助软件下载...
必备教程!九酷众游辅助,hhp... 必备教程!九酷众游辅助,hhpoker一直输有挂吗,技巧教程(有挂头条);九酷众游辅助是一款益智类棋...