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脚本,娱家麻将多功能切实真的是有透视器脚本1、娱家麻将辅助软件下载...
重大消息开挂!家乡大贰开挂AI... 重大消息开挂!家乡大贰开挂AI插件,家乡大贰多功能果然有开挂插件家乡大贰脚本下载中分为三种模型:家乡...
玩家必看科普外挂!河洛杠次外挂... 玩家必看科普外挂!河洛杠次外挂AI工具,河洛杠次多功能一直是真的有外挂工具亲,关键说明,河洛杠次透视...
重大来袭开挂!广西老友玩开挂A... 重大来袭开挂!广西老友玩开挂AIapp,广西老友玩多功能竟然有开挂app1、每一步都需要思考,不同水...
科技揭秘透视挂!嘟咪互动麻将透... 科技揭秘透视挂!嘟咪互动麻将透视挂AI插件,嘟咪互动麻将多功能竟然是有透视挂插件1、玩家可以在嘟咪互...
盘点几款辅助器!微乐河北麻将辅... 盘点几款辅助器!微乐河北麻将辅助器AI工具,微乐河北麻将多功能竟然存在有辅助器工具盘点几款辅助器!微...
盘点一款透视挂!星际互联透视挂... 盘点一款透视挂!星际互联透视挂AI方法,星际互联多功能竟然是有透视挂方法1、星际互联破解器简单,星际...
推荐十款开挂!MG棋牌开挂AI... 推荐十款开挂!MG棋牌开挂AI教程,MG棋牌多功能一直是真的有开挂教程1、每一步都需要思考,不同水平...
记者揭秘辅助器!大圣天辅助器A... 记者揭秘辅助器!大圣天辅助器AI教程,大圣天多功能切实真的有辅助器教程1)大圣天免费钻石:进一步探索...
技术分享作弊器!赣牌圈打炸作弊... 技术分享作弊器!赣牌圈打炸作弊器AI插件,赣牌圈打炸多功能确实是有作弊器插件1、起透看视 赣牌圈打炸...