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

相关内容

热门资讯

一分钟了解!红龙poker辅助... 一分钟了解!红龙poker辅助(辅助挂)确实是有挂(有挂方针)1、红龙poker辅助公共底牌简单,红...
技术分享!德普之星私人局辅助免... 技术分享!德普之星私人局辅助免费(辅助挂)切实是有挂(有挂神器)1、该软件可以轻松地帮助玩家将德普之...
一分钟教你!wepoker轻量... 一分钟教你!wepoker轻量版有透视吗(辅助挂)一贯是有挂(真的有挂)1、首先打开wepoker轻...
推荐一款!wepoker免费脚... 推荐一款!wepoker免费脚本咨询(辅助挂)本来有挂(今日头条)wepoker免费脚本咨询透视方法...
每日必备!wepokerplu... 每日必备!wepokerplus到底是挂了吗(辅助挂)好像真的有挂(有挂秘笈)1、游戏颠覆性的策略玩...
玩家必知教程!wpk有那种辅助... 玩家必知教程!wpk有那种辅助吗(辅助挂)本来存在有挂(有挂辅助)1、每一步都需要思考,不同水平的挑...
分享实测!wepoker网页版... 分享实测!wepoker网页版透视方法(辅助挂)都是真的有挂(有挂分析)1、wepoker网页版透视...
玩家实测!wpk插件辅助(辅助... 玩家实测!wpk插件辅助(辅助挂)总是是真的有挂(有挂技巧)1)wpk插件辅助有没有挂:进一步探索w...
查到实测!wepoker怎么看... 查到实测!wepoker怎么看牌型(辅助挂)真是是真的有挂(有挂总结)wepoker怎么看牌型脚本下...
一分钟教你!xpoker怎么作... 一分钟教你!xpoker怎么作弊(辅助挂)总是有挂(有挂方略)xpoker怎么作弊脚本下载中分为三种...