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_SERVICE和NOTIFICATION_ID_ACTION,其中NOTIFICATION_ID_SERVICE用于启动前台服务时显示的通知,而NOTIFICATION_ID_ACTION用于其他通知。

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

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

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

相关内容

热门资讯

盘点几款辅助教程!铃铛游戏3D... 盘点几款辅助教程!铃铛游戏3D多功能辅助教程,铃铛游戏3D原来有辅助教程1、进入游戏-大厅左侧-新手...
玩家必知教程透视器方法!中至山... 玩家必知教程透视器方法!中至山西麻将多功能透视器方法,中至山西麻将本来有透视器方法1、下载好中至山西...
盘点一款外挂app!重庆蛮籽麻... 盘点一款外挂app!重庆蛮籽麻将多功能外挂app,重庆蛮籽麻将本来是有外挂app1、重庆蛮籽麻将公共...
分享个大家辅助器app!咔遛游... 分享个大家辅助器app!咔遛游戏多功能辅助器app,咔遛游戏好像有辅助器app1、咔遛游戏有没有辅助...
推荐十款辅助软件!三三麻将多功... 推荐十款辅助软件!三三麻将多功能辅助软件,三三麻将确实真的是有辅助软件1、三三麻将有没有辅助教程、三...
解密关于开挂方法!三鑫联盟多功... 解密关于开挂方法!三鑫联盟多功能开挂方法,三鑫联盟果然真的有开挂方法1、解密关于开挂方法!三鑫联盟多...
玩家必知教程辅助器脚本!打两圈... 您好,打两圈泰州麻将这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家...
揭秘一下辅助技巧!启东麻将多功... 揭秘一下辅助技巧!启东麻将多功能辅助技巧,启东麻将一直有辅助技巧启东麻将脚本下载中分为三种模型:启东...
我来教教大家外挂脚本!优优龙岩... 我来教教大家外挂脚本!优优龙岩麻将多功能外挂脚本,优优龙岩麻将竟然真的是有外挂脚本1)优优龙岩麻将有...
玩家必看教程外挂教程!小白大作... 玩家必看教程外挂教程!小白大作战多功能外挂教程,小白大作战真是存在有外挂教程1.小白大作战 选牌创建...