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

相关内容

热门资讯

玩家必知教程透视挂!金花佛手辅... 玩家必知教程透视挂!金花佛手辅助透视挂,胡乐白银麻将真的有外挂,总是有挂技巧1、金花佛手脚本辅助下载...
据权威媒体报道开挂!510k辅... 据权威媒体报道开挂!510k辅助透视挂,中至乐平麻将存在有外挂,一直有挂秘笈据权威媒体报道开挂!51...
这一现象值得深思辅助!安乡偎麻... 这一现象值得深思辅助!安乡偎麻雀辅助透视挂,网趣沈阳麻将是真的有外挂,一直有挂方略一、网趣沈阳麻将游...
三分钟了解开挂!新二号辅助透视... 三分钟了解开挂!新二号辅助透视挂,战地是真的有外挂,确实有挂功能1、新二号破解器简单,新二号机器人操...
推荐十款辅助!陕西欢喜辅助透视... 推荐十款辅助!陕西欢喜辅助透视挂,莫旗是有外挂,切实有挂秘笈1、起透看视 陕西欢喜辅助软件价格2、随...
一分钟教会你开挂!加加辅助透视... 一分钟教会你开挂!加加辅助透视挂,老友记确实有外挂,切实确实有挂1、老友记公共底牌简单,老友记透视插...
新手必备开挂!逐月辅助透视挂,... 新手必备开挂!逐月辅助透视挂,畅享睢宁麻将是有外挂,切实真实有挂1、完成畅享睢宁麻将有辅助插件,帮助...
实测交流开挂!星悦云南麻将辅助... 实测交流开挂!星悦云南麻将辅助透视挂,家家乐牌吧存在有外挂,确实有挂教学1、打开软件启动之后找到中间...
一分钟了解开挂!久久娱乐辅助透... 一分钟了解开挂!久久娱乐辅助透视挂,临沧麻将存在有外挂,一直有挂头条1、下载好久久娱乐透视辅助下载之...
有玩家发现辅助!微乐家乡麻将辅... 有玩家发现辅助!微乐家乡麻将辅助透视挂,椰城真的有外挂,确实存在有挂亲,关键说明,微乐家乡麻将透视脚...