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破解,详细插件(有挂解惑)1、指导大家!崇阳斗棋辅助脚本视...
必备攻略!聚友联盟辅助器,雀友... 必备攻略!聚友联盟辅助器,雀友会潮汕麻雀总部辅助,详细技巧(有挂实锤)1、雀友会潮汕麻雀总部辅助公共...
每日必备!随意玩免费辅助器有挂... 每日必备!随意玩免费辅助器有挂吗,四川家园茶馆辅助,详细脚本(有挂秘笈)1、随意玩免费辅助器有挂吗脚...
分享认知!雀友会潮汕图片辅助器... 分享认知!雀友会潮汕图片辅助器,边锋老友怎么开辅助,详细挂(有挂功能)1、雀友会潮汕图片辅助器破解器...
解密关于!八闽福建辅助,开心庄... 解密关于!八闽福建辅助,开心庄园辅助器免费,详细教程(真实有挂)开心庄园辅助器免费透视方法中分为三种...
玩家必看分享!福州天天开心辅助... 玩家必看分享!福州天天开心辅助,微乐山西小程序破解器,详细挂(有挂方法)一、微乐山西小程序破解器可以...
玩家攻略!互游辅助脚本,微乐山... 玩家攻略!互游辅助脚本,微乐山西脚本插件,详细脚本(有挂存在)1、微乐山西脚本插件辅助软件下载优化,...
攻略讲解!博雅棋牌辅助器,三哥... 攻略讲解!博雅棋牌辅助器,三哥玩十三张辅助器,详细方法(有挂总结)1、玩家可以在三哥玩十三张辅助器透...
重大通报!海贝之城辅助器,雀友... 重大通报!海贝之城辅助器,雀友会潮汕麻将开挂辅助,详细方法(有挂方针)1、该软件可以轻松地帮助玩家将...
玩家必知教程!途游四川辅助器是... 玩家必知教程!途游四川辅助器是真的吗,胡乐辅助脚本是真的假的,详细工具(有挂讲解)玩家必知教程!途游...