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

相关内容

热门资讯

三分钟了解!微信老友广东辅助器... 三分钟了解!微信老友广东辅助器,朋朋政和麻将为什么一直输,详细器(有挂教程)一、朋朋政和麻将为什么一...
推荐一款!菠萝神辅助器app,... 推荐一款!菠萝神辅助器app,雀友会广东潮汕辅助透视,详细器(新版有挂)1、完成雀友会广东潮汕辅助透...
必备科技!微信小程序微乐辅助器... 必备科技!微信小程序微乐辅助器,老友麻将开挂辅助器,详细神器(有挂总结)1、让任何用户在无需微信小程...
玩家必看教程!jj斗地主麻将外... 玩家必看教程!jj斗地主麻将外挂,欢乐达人正版脚本,详细软件(有挂详细)1、完成欢乐达人正版脚本辅助...
详细说明!微乐河南小程序微乐辅... 详细说明!微乐河南小程序微乐辅助脚本,蛮王大厅辅助插件,详细工具(今日头条)该软件可以轻松地帮助玩家...
专业讨论!传送屋软件辅助,皮皮... 专业讨论!传送屋软件辅助,皮皮透视辅助软件工具,详细软件(有挂秘诀)1、超多福利:超高返利,海量正版...
查到实测辅助!途游休闲铺破解版... 查到实测辅助!途游休闲铺破解版,789大菠萝可以控制吗,详细挂(有挂技巧)该软件可以轻松地帮助玩家将...
盘点几款!奇迹掌趣辅助,科乐填... 盘点几款!奇迹掌趣辅助,科乐填大坑破解版安卓,详细工具(真是有挂)1、完成科乐填大坑破解版安卓有辅助...
避坑细节!悟空大厅辅助器,广东... 您好,广东闲来辅助视频这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩...
今日头条!打两圈辅助,边锋辅助... 今日头条!打两圈辅助,边锋辅助器小程序微信,详细器(有挂功能)1、边锋辅助器小程序微信免费辅助多个强...