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可以确保前台服务通知和其他通知

相关内容

热门资讯

透视辅助“打哈儿麻将辅助软件”... 您好:打哈儿麻将辅助软件这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的...
我来教教大家“闲来辅助神器下载... 我来教教大家“闲来辅助神器下载2022”先前有辅助开挂插件(的确有挂);无需打开直接搜索微信(136...
透视真的“中至窝龙辅助”素来有... 透视真的“中至窝龙辅助”素来有辅助开挂插件(揭秘有挂);无需打开直接搜索微信(136704302)咨...
透视软件“中至余干万能辅助器”... 透视软件“中至余干万能辅助器”先前有辅助开挂脚本(有挂工具) 了解更多开挂安装加(136704302...
透视真的“酷玩联盟破解版下载”... 透视真的“酷玩联盟破解版下载”素来有辅助工具(了解有挂);无需打开直接搜索加(薇:136704302...
一分钟快速了解“兴动海满辅助”... 您好:兴动海满辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
技术分享“赣牌圈控制牌型”本然... 技术分享“赣牌圈控制牌型”本然有开挂辅助器(有挂总结);无需打开直接搜索加薇136704302(咨询...
总算了解“pokemmo免费脚... 总算了解“pokemmo免费脚本”本然有开挂辅助软件(确实有挂);无需打开直接搜索加(薇:13670...
推荐攻略“兴动互娱修改器免费”... 推荐攻略“兴动互娱修改器免费”素来有辅助开挂软件(讲解有挂) 了解更多开挂安装加(136704302...
新手必备“陕西三代辅助”往昔有... 新手必备“陕西三代辅助”往昔有辅助安装(有挂猫腻) 了解更多开挂安装加(136704302)微信号陕...