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、进入到腾旭欢乐全集辅...
推荐一下!广东闲来辅助视频,约... 推荐一下!广东闲来辅助视频,约战竞技场辅助软件,总是真的是有挂(哔哩哔哩)1、起透看视 广东闲来辅助...
分享一下!手机科乐辅助,川娱竞... 分享一下!手机科乐辅助,川娱竞技血战辅助,真是是有挂(哔哩哔哩)运川娱竞技血战辅助辅助工具,进入游戏...
了解一下!点点游戏辅助,开心泉... 了解一下!点点游戏辅助,开心泉州辅助免费下载,一贯有挂(哔哩哔哩)1、点点游戏辅助透视辅助软件激活码...
推荐一下!德友汇辅助,拱趴游戏... 推荐一下!德友汇辅助,拱趴游戏插件,真是真的有挂(哔哩哔哩)一、德友汇辅助游戏安装教程牌型概率发牌机...
解迷一下!微信闲来辅助神器免费... 解迷一下!微信闲来辅助神器免费,决战卡五星作弊,竟然真的有挂(哔哩哔哩)亲,关键说明,微信闲来辅助神...
有挂一下!潮汕激k辅助器,赣牌... 有挂一下!潮汕激k辅助器,赣牌圈修改器,本来真的是有挂(哔哩哔哩)一、潮汕激k辅助器可以开透视的定义...
解谜一下!广麻辅助器,玩吧辅助... 您好,广麻辅助器这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这...