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好... 第二分钟辅助!wepoker好友助力码,wepoker透视辅助下载,指引教程(有挂助手)1、wepo...
六分钟辅助!wpk模拟器,hh... 六分钟辅助!wpk模拟器,hhpoker的辅助是真的吗,策略教程(新版有挂)1、hhpoker的辅助...
9分钟辅助!微扑克微乐辅助,w... 9分钟辅助!微扑克微乐辅助,wepoker私人局辅助器怎么用,指引教程(有挂方略)1、用户打开应用后...
第1分钟辅助!wepoker免... 第1分钟辅助!wepoker免费脚本咨询,hhpoker有没有作弊挂,举措教程(有挂实锤)1、起透看...
第十分钟辅助!cloudpok... 第十分钟辅助!cloudpoker作弊,wepoker模拟器哪个好用,大纲教程(了解有挂)1.wep...
第5分钟辅助!fishpoke... 第5分钟辅助!fishpoker透视,epoker透视,妙招教程(真是有挂)1、金币登录送、破产送、...
第九分钟辅助!wpk有那种辅助... 第九分钟辅助!wpk有那种辅助吗,wepoker私人局规律,窍门教程(有挂教学)亲,关键说明,wep...
第2分钟辅助!wepoker私... 第2分钟辅助!wepoker私人局辅助,wepoker作弊视频,举措教程(有挂技巧)1、第2分钟辅助...
第6分钟辅助!德普之星辅助器,... 第6分钟辅助!德普之星辅助器,德州透视插件,资料教程(有挂总结)1、在德普之星辅助器插件功能辅助器技...
第二分钟辅助!wepoker免... 第二分钟辅助!wepoker免费透视,wepoker免费透视,举措教程(有挂解密)1、许多玩家不知道...