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

相关内容

热门资讯

一秒答解透视!心悦填大坑ai透... 一秒答解透视!心悦填大坑ai透视多功能插件,一贯是有透视插件法子是一款可以让一直输的玩家,快速成为一...
2027版辅助器!熊猫互娱辅助... 2027版辅助器!熊猫互娱辅助器多功能AI工具,熊猫互娱真是有辅助器工具1)熊猫互娱辅助插件:进一步...
WPK链接透视手册!wepok... WPK链接透视手册!wepoker养号规律,AApokerai多功能透视教程1、操作简单,无需wep...
必看攻略辅助!川麻圈ai辅助多... 必看攻略辅助!川麻圈ai辅助多功能插件,果然真的有辅助下载方式;近期一款 AI 黑科技工具火爆出圈,...
2027版辅助!北方家乡麻将辅... 2027版辅助!北方家乡麻将辅助多功能AI脚本,北方家乡麻将果然是有辅助脚本1、首先打开北方家乡麻将...
WPK辅助透视妙计!wepok... WPK辅助透视妙计!wepoker插件下载,红龙扑克ai多功能透视神器WPK辅助透视妙计!wepok...
科技介绍辅助器!科米台州麻将a... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
2027版辅助!麻辣竞技辅助多... 2027版辅助!麻辣竞技辅助多功能AI攻略,麻辣竞技一贯真的是有辅助攻略1、麻辣竞技透视辅助软件激活...
WPK购买辅助器学习!如何下载... WPK购买辅助器学习!如何下载wpk透视版,AApokerai多功能辅助器脚本1、该软件可以轻松地帮...
来一盘辅助器!和和林甸麻将ai... 来一盘辅助器!和和林甸麻将ai辅助器多功能插件,本来有辅助器教程法子主打赛后复盘的 AI 游戏攻略黑...