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

相关内容

热门资讯

WPK多开方式!wpk透视是真... WPK多开方式!wpk透视是真的假的,微扑克ai多功能开挂技巧1、每一步都需要思考,不同水平的挑战w...
正版作弊器人人烟台麻将作弊器多... 正版作弊器人人烟台麻将作弊器多功能AI技巧,人人烟台麻将一贯是真的有作弊器技巧1、全新机制【人人烟台...
WPK下载诀窍!拱趴大菠萝有什... WPK下载诀窍!拱趴大菠萝有什么挂,wpkai多功能辅助器插件在进入拱趴大菠萝有什么挂软件靠谱后,参...
正版辅助器比鸡麻将辅助器多功能... 正版辅助器比鸡麻将辅助器多功能AI教程,比鸡麻将一贯是有辅助器教程1、比鸡麻将免费脚本咨询教程、比鸡...
WPK透视版演示!德扑之星辅助... WPK透视版演示!德扑之星辅助器,微扑克ai多功能开挂插件1、操作简单,无需德扑之星辅助器手机版透视...
正版外挂纵横互娱外挂多功能AI... 正版外挂纵横互娱外挂多功能AI方法,纵横互娱总是有外挂方法纵横互娱透视方法中分为三种模型:纵横互娱靠...
WPK靠谱步骤!大菠萝辅助器,... WPK靠谱步骤!大菠萝辅助器,微扑克ai多功能辅助技巧1、完成大菠萝辅助器辅助器v3.3的残局,帮助...
正版辅助新时代辅助多功能AI脚... 正版辅助新时代辅助多功能AI脚本,新时代总是存在有辅助脚本1、进入到新时代是否有挂之后,能看到左侧胜...
WPK官网积累!wpk软件是真... WPK官网积累!wpk软件是真的吗,菠萝智星德州ai多功能透视挂技巧1、WPK官网积累!wpk软件是...
正版开挂微友麻将开挂多功能AI... 正版开挂微友麻将开挂多功能AI脚本,微友麻将确实真的是有开挂脚本1、微友麻将辅助软件下载优化,微友麻...