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

相关内容

热门资讯

推荐十款!微信小程序功夫川麻小... 您好:微信小程序功夫川麻小程序辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现...
发现玩家!全民牛牛拼三张开挂,... 发现玩家!全民牛牛拼三张开挂,wepoker底牌透视,安装教程(存在有挂);最新版2026是一款经典...
发现玩家!老k游戏辅助,hhp... 发现玩家!老k游戏辅助,hhpoker外挂靠谱吗,新2025版(有挂透视)是一款可以让一直输的玩家,...
分享一款!新超凡大厅辅助,hh... 分享一款!新超凡大厅辅助,hhpoker可以控制吗,切实教程(有挂辅助);1、在新超凡大厅辅助ai机...
程序员教你!微信辅助工具,哈糖... 程序员教你!微信辅助工具,哈糖大菠萝攻略,细节方法(真的有挂);1、这是跨平台的微信辅助工具黑科技,...
一分钟了解!广东雀神挂件脚本开... 一分钟了解!广东雀神挂件脚本开挂,pokemmo辅助脚本,wpk教程(有挂分享);【福星临门,好运相...
大神推荐!广西老友玩游戏辅助器... 大神推荐!广西老友玩游戏辅助器,微扑克辅助,新2025版(有挂透视);一、广西老友玩游戏辅助器AI软...
必备教程!科乐天天踢辅助视频,... 必备教程!科乐天天踢辅助视频,来玩app破解,详细教程(的确有挂);科乐天天踢辅助视频软件透明挂作为...
盘点一款!凑一桌开挂,wepo... 盘点一款!凑一桌开挂,wepoker透视底牌,透明挂教程(有挂透视);1、超多福利:超高返利,海量正...
玩家交流!518互游辅助,we... 玩家交流!518互游辅助,wepoker新号好一点吗,我来教教你(有挂攻略);最新版2026是一款经...