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

相关内容

热门资讯

透视中牌率!wepoker辅助... 透视中牌率!wepoker辅助是真的假的,wepoker线上大神(详细有机器人)1、起透看视 wep...
透视安卓版!wepoker透视... 透视安卓版!wepoker透视方法,有没有人wepoker(详细透视脚本免费下载)1)有没有人wep...
透视有挂!we-poker正规... 透视有挂!we-poker正规吗,wepoker轻量版书签(详细免费辅助器);1、用户打开应用后不用...
透视插件!wepoker怎么设... 透视插件!wepoker怎么设置盖牌,wepoker买钻石有用吗(详细可以开辅助)1、金币登录送、破...
透视私人局!wepoker怎么... 透视私人局!wepoker怎么拿到好牌,poker辅助器免费安装(详细有透视功能)poker辅助器免...
透视玄学!wepoker怎么挂... 透视玄学!wepoker怎么挂飞机,wejoker辅助机器人(详细透视底牌);该软件可以轻松地帮助玩...
透视存在!wepoker怎么挂... 透视存在!wepoker怎么挂底牌,we-poker软件(详细辅助器下载)1、完成wepoker怎么...
透视ai代打!安装不了wepo... 透视ai代打!安装不了wepoker,wepoker私人局外卦(详细私人局规律)1、全新机制【wep...
透视私人局!we poker辅... 透视私人局!we poker辅助器,WePoKer辅助器(详细可以看牌型)1、进入游戏-大厅左侧-新...
透视教学!wepoker有没有... 透视教学!wepoker有没有辅助,we-poker辅助器(详细国外版透视)1、在wepoker有没...