Oreo - 前台服务不显示前台通知

2024-03-04

到目前为止,我已经调整了我的代码以使用ContextCompat.startForegroundService(context, intentService);开始我的服务。这样,它就可以在 android

我仍然看到了差异,在 android oreo 中我没有看到我的自定义前台通知(我只看到“应用程序正在后台运行”通知)。我还需要在那里调整什么吗?

我的服务如下所示:

public class BaseOverlayService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        moveToForeground();
    }

    private void moveToForeground() {
        Notification notification = ...;
        super.startForeground(NOTIFICATION_ID, notification);
    }
}

官方示例

这个例子(https://github.com/googlesamples/android-play-location/blob/master/LocationUpdatesForegroundService/app/src/main/java/com/google/android/gms/location/sample/locationupdatesforegroundservice/LocationUpdatesService.java#L200 https://github.com/googlesamples/android-play-location/blob/master/LocationUpdatesForegroundService/app/src/main/java/com/google/android/gms/location/sample/locationupdatesforegroundservice/LocationUpdatesService.java#L200)显示为评论,我应该使用以下内容,但是startServiceInForeground不存在...

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
    NotificationManager mNotificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
    mNotificationManager.startServiceInForeground(new Intent(this, BaseOverlayService.class), NOTIFICATION_ID, notification);
} else {
    startForeground(NOTIFICATION_ID, notification);
}

Edit

我的通知是这样创建的,到目前为止,它正在数千个 API

protected Notification foregroundNotification(int notificationId)
{
    boolean paused = MainApp.getPrefs().sidebarServicePaused();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.icon_not);
    builder.setContentIntent(notificationIntent());
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
    builder.setContentTitle(getString(R.string.derived_app_name));
    builder.setContentText(getString(checkStatus() ? (paused ? R.string.service_notification_text_paused : R.string.service_notification_text_running) : R.string.service_notification_text_preparing));
    builder.setColor(Color.parseColor("#25baa2"));

    if (MainApp.getPrefs().hideNotificationIcon())
        builder.setPriority(NotificationCompat.PRIORITY_MIN);
    else
        builder.setPriority(NotificationCompat.PRIORITY_MAX);

    if (paused)
        builder.addAction(R.drawable.ic_play_arrow_black_24dp, getString(R.string.resume), resumeIntent(this));
    else
        builder.addAction(R.drawable.ic_pause_black_24dp, getString(R.string.pause), pauseIntent(this));

    return builder.build();
}

您可能需要为您的应用程序定义通知渠道。检查日志,应该有警告。 查看this https://medium.com/exploring-android/exploring-android-o-notification-channels-94cd274f604c用于介绍

我给你举个例子,它是用 kotlin 编写的。 首先,创建一个这样的类。您需要在应用程序启动时调用一次 createNotificationChannel()。

class NotificationManager(private val context: Context) {

    companion object {
        private val CHANNEL_ID = "YOUR_CHANNEL_ID"
        private val CHANNEL_NAME = "Your human readable notification channel name"
        private val CHANNEL_DESCRIPTION = "description"
    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun getMainNotificationId(): String {
        return CHANNEL_ID
    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun createMainNotificationChannel() {
            val id = CHANNEL_ID
            val name = CHANNEL_NAME
            val description = CHANNEL_DESCRIPTION
            val importance = android.app.NotificationManager.IMPORTANCE_LOW
            val mChannel = NotificationChannel(id, name, importance)
            mChannel.description = description
            mChannel.enableLights(true)
            mChannel.lightColor = Color.RED
            mChannel.enableVibration(true)
            val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager
            mNotificationManager.createNotificationChannel(mChannel)
    }
}

然后你可以像这样使用util

fun createNotificationCompatBuilder(context: Context): NotificationCompat.Builder {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        return NotificationCompat.Builder(context, NotificationManager(context).mainNotificationId)
    } else {
        return NotificationCompat.Builder(context)
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Oreo - 前台服务不显示前台通知 的相关文章

随机推荐

  • 配置 webpack 以允许浏览器调试

    我是 webpack 的新手 我正在转换现有的 Web 应用程序以使用它 我正在使用 webpack 来捆绑和缩小我的 JS 这在部署时非常有用 但这使得在开发过程中调试非常困难 通常我使用 chrome 内置的调试器来调试 JS 问题 或
  • SQL Server 探查器

    当您使用 SQL Server 探查器时 持续时间和 CPU 之间有什么区别 我知道默认情况下两者都以毫秒显示 CPU 是运行查询所需的 CPU 时间 而持续时间更像是查询返回任何数据所需的时间 根据我的理解 如果存在任何阻塞等 您的 CP
  • 如何在Java中使用PDFBox获取PDF文件中的所有书签

    我是 Apache PDFbox 的新手 我想使用Java中的PDFBox库提取PDF文件中的所有书签 知道如何提取它们吗 来自在源码下载中 PDDocument document PDDocument load new File PDDo
  • 将表从一个数据库复制到另一个数据库的最简单方法?

    当数据库位于不同用户下时 将数据从一个数据库中的表复制到另一个数据库中的表的最佳方法是什么 我知道我可以使用 INSERT INTO database2 table2 SELECT from database1 table1 但这里的问题是
  • Android 应用程序通过移动端口使用 P2P 模式流式传输 IP 摄像机?

    我正在尝试通过 Android 应用程序上的 IP 摄像头流式传输视频 我已经使用以下方法成功获取了视频 使用媒体播放器和 Surface View 的 RTSP 使用视频视图的 RTSP 重定向到 VLC 以流式传输视频 重定向到本机 用
  • 实际示例使用 dup 或 dup2

    我知道什么dup dup2 http www opengroup org onlinepubs 009695399 functions dup html可以 但是不知道什么时候会用到 有什么实际例子吗 示例之一是 I O 重定向 为此 您分
  • android sdk 位置不能位于文件系统根目录

    我已经将 Android Studio 安装在 F 驱动器中 我的 Flutter 项目位于 E 驱动器中 Flutter插件安装在Android Studio中 但是当我在 Android Studio 中打开项目并转到 SDK 管理器时
  • 在具有自动高度的 IFrame 中获取 Google 表单

    所以我有一个用 Google Docs 构建的表单 因为它看起来比从头开始更容易 我通过将代码从谷歌页面复制粘贴到我的域上的页面来实现这一点 我设法用我在这里找到的这个可爱的小脚本让它自动调整其高度 http www frontpagewe
  • 仅在给定用户 ID 的情况下选择固定范围高分的有效方法

    我有一个表格 其中包含每个球员参加过的每场比赛的记录 从这个问题的最佳答案我已经有了一个很好的开始 百万条目排名 https stackoverflow com questions 5436263 ranking with millions
  • 检测android中的home按钮按下

    这已经让我发疯有一段时间了 有没有什么方法可以可靠地检测 Android 应用程序中是否按下了主页按钮 如果做不到这一点 是否有一种可靠的方法来判断是什么导致活动进入 onPause 即我们能否检测到它是否是由新活动启动或按返回 主页引起的
  • AWS DynamoDB - 如何在 1 次调用中实现:如果集合存在,则添加要设置的值 - 或者用值实例化集合?

    我有一个用户表 有一个名为 朋友 的属性 它将是用户所有朋友的 id 的集合 最初 我尝试在创建用户时将 Friends 属性实例化为空集 但收到错误消息 指出不能有空属性 因此 如果某人还没有朋友 我能找到的唯一解决方案是读取用户的属性
  • 内联插件不起作用

    根据Chart js 文档 http www chartjs org docs latest developers plugins html 以下代码应该可以工作 new Chart document getElementById char
  • Github 操作:在单个运行器中运行多个作业或在作业之间共享工作空间

    有什么方法可以在单个运行器中运行多个作业或在作业之间共享 Github 操作工作区 在我的组织中 开发团队使用由多个团队创建和管理的多个可重用工作流程 团队build创建和管理build yaml构建应用程序 我的团队创建并管理analys
  • 我可以在 Microsoft Access VBA 中使用变量作为控件的名称吗

    我有一个 Microsoft Access 弹出表单 用于查找地址 一旦用户找到邮政编码 该地址就会被放入其启动表单的各个文本框中 问题是 这个弹出表单是从整个数据库中的各种表单启动的 因此它将结果放入的文本框位于不同的位置 我尝试通过以下
  • 使用 Python 解析电子邮件

    我正在编写一个 Python 脚本来处理从Procmail http www procmail org 正如本文中所建议的question https stackoverflow com questions 1225586 checking
  • “Fluent Assertion”和“Should Assertion Library”的区别

    有人可以指出差异吗 以上确实是我的问题 但您是否也可以与他们分享您的经验以及为什么您使用其中之一 它们只是两个不同的库 因此只需查看功能 尤其是报告功能 然后进行选择 由于我是作者流畅的断言 我显然有偏见 不管怎样 我对我们为使错误消息尽可
  • 使用户能够在 Cognito 中重置过期的临时密码

    我知道管理员可以使用以下命令从控制台重置用户的临时密码 aws cognito idp admin create user region us east 1 user pool id us east 1 youruserpoolid use
  • 如何在Python中模拟sqlite3.connect

    我使用的是Python 3 3 在Anaconda环境下 我想模拟 sqlite3 connect 例如在MyTests 见下文 我想要test sqlite3 connect返回字符串connection而不是实际的sqlite3 Con
  • PHP 替换大括号之间的所有内容?

    我对preg很烂 我永远不会学它 这应该不难 我可以提供一个代码示例来替换大括号之间的所有内容 包括空格 所有内容吗 比如 string preg replace si string 或者其他的东西 你错过了最初的 分隔符 s 看看它在线工
  • Oreo - 前台服务不显示前台通知

    到目前为止 我已经调整了我的代码以使用ContextCompat startForegroundService context intentService 开始我的服务 这样 它就可以在 android 我仍然看到了差异 在 android