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

相关内容

热门资讯

插件辅助挂!aapoker透视... 插件辅助挂!aapoker透视脚本下载,德普之星怎么设置埋牌,竟然是有挂(有挂辅助)1、点击下载安装...
详细说明!雀神广东定制插件辅助... 详细说明!雀神广东定制插件辅助,欢乐达人猜猜乐辅助,详细脚本(真的有挂)1、雀神广东定制插件辅助有没...
相较于以往!悦扑克脚本,德普之... 相较于以往!悦扑克脚本,德普之星透视软件免费入口官网,真是是有挂(有挂秘籍)1)德普之星透视软件免费...
玩家必看科普!丽水都莱脚本,吉... 玩家必看科普!丽水都莱脚本,吉祥填大坑有插件吗,详细工具(确实有挂)1、金币登录送、破产送、升级送、...
据悉!德州透视插件,德扑之星怎... 据悉!德州透视插件,德扑之星怎么设置埋牌,原来是真的有挂(真是有挂)小薇(辅助器软件下载)致您一封信...
来一盘!开心泉州辅助器,吉祥填... 来一盘!开心泉州辅助器,吉祥填大坑游戏攻略,详细神器(揭秘有挂)1、每一步都需要思考,不同水平的挑战...
近年来!wepoker数据分析... 近年来!wepoker数据分析工具,德普之星透视辅助软件下载,确实是真的有挂(有人有挂)1、每一步都...
一分钟揭秘!钱柜手游辅助,创思... 一分钟揭秘!钱柜手游辅助,创思维激k透视插件,详细攻略(有挂透视)1、在创思维激k透视插件插件功能辅...
不少玩家反映!wepoker透... 您好,德扑之星私人局透视这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多...
揭秘攻略!全民牛牛拼三张开挂,... 揭秘攻略!全民牛牛拼三张开挂,河南微乐小程序辅助器免费,详细攻略(有挂神器)一、河南微乐小程序辅助器...