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

相关内容

热门资讯

透视好友!哈糖大菠萝免费辅助器... 透视好友!哈糖大菠萝免费辅助器(透视)创思维激k辅助器下载(果然有辅助修改器)-哔哩哔哩1、任何哈糖...
透视规律!wepoker永久免... 透视规律!wepoker永久免费脚本(透视)皮皮胡子辅助(总是有辅助app)-哔哩哔哩一、皮皮胡子辅...
不少玩家反映!newpoker... 不少玩家反映!newpoker脚本(透视)悟空大厅辅助看牌(本来是真的辅助软件)-哔哩哔哩1、完成悟...
迎来新发展!如何判断wpk辅助... 迎来新发展!如何判断wpk辅助软件的真假(透视)海贝之城有辅助吗(其实是有辅助插件)-哔哩哔哩1、用...
今天下午!德州透视插件(透视)... 今天下午!德州透视插件(透视)樱花之盛挂(竟然存在有辅助辅助器)-哔哩哔哩樱花之盛挂脚本下载中分为三...
透视好友!wepokerh5破... 透视好友!wepokerh5破解(透视)微乐河南小程序微乐辅助脚本(好像存在有辅助软件)-哔哩哔哩1...
透视神器!we-poker辅助... 透视神器!we-poker辅助软件教程(透视)新广西老友辅助(切实是有辅助下载)-哔哩哔哩新广西老友...
透视总结!哈糖大菠萝能开挂吗(... 透视总结!哈糖大菠萝能开挂吗(透视)多乐破解游戏盒子破解版(都是有辅助软件)-哔哩哔哩1、多乐破解游...
围绕透视问题!aapoker能... 您好,南通长牌有挂吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家...
据目击者称!wepoker代打... 据目击者称!wepoker代打辅助(透视)微信闲来辅助神器免费(果然存在有辅助插件)-哔哩哔哩小薇(...