android - 通知未显示,因为服务或警报管理器已被终止
问题描述
我有一个无法解决的问题,我今天花了 7 个小时试图找到解决方案。我正在尝试为我的应用显示通知。它在具有旧操作系统版本的旧设备上运行良好,但是在运行 Oreo API 27 的 Note 8 上,我的通知没有显示。我花了几个小时的测试,我发现了问题:当应用程序正在运行并且应用程序打开时,会显示通知,但是当我退出应用程序时,通知不会显示。所以我认为这与系统处理服务的方式有关。
这是正常行为吗?有没有解决的办法?
这是我的通知代码:
onCreate...
if (settings.getBoolean("enabled", true)) {
if (settings.getLong("lastRun", Long.MAX_VALUE) < System.currentTimeMillis() - mTimeDelay) {
sendNotification();
}
}
setAlarm();
stopSelf();
}
public void setAlarm() {
Intent serviceIntent = new Intent(this, CheckRecentRun.class);
PendingIntent pi = PendingIntent.getService(this, 131313, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + mTimeDelay, pi);
//Alarm is set for 3 days
}
public void sendNotification() {
Intent intent = new Intent(this, WelcomeActivity.class);
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = notificationManager.getNotificationChannel(channelId);
if (mChannel == null) {
mChannel = new NotificationChannel(channelId, channelName, importance);
mChannel.setDescription(mNotificationText);
mChannel.enableLights(false);
mChannel.setLightColor(Color.GREEN);
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelId).setStyle(new NotificationCompat.BigTextStyle().bigText(mNotificationText)).setSmallIcon(R.mipmap.ic_launcher).setContentTitle(mAppName).setContentText(mNotificationText).setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(false);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(mNotificationCode, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
notificationManager.notify(notificationId, mBuilder.build());
}
解决方案
这可能是三星在其新款手机上激进的电池管理行为(在华为等许多其他品牌中)扼杀了您的应用程序。
看看这个网站:https ://dontkillmyapp.com/
并查看三星下的注释:
Galaxy S8 及更高版本 随着旗舰 Galaxy S8 的推出(以及一些早期的实验),三星推出了一种有缺陷的延长电池寿命的尝试,称为 App 电源监视器。
为了让您的应用程序正常运行,请在应用程序电源监视器中将它们列入白名单。
怎么做:
打开设置 > 设备维护 > 电池,在底部您会看到最常用的应用程序列表。您可以通过选择它们然后点击大的保存电源按钮来单独或在组中管理应用程序。处于睡眠状态的应用程序将出现在底部的睡眠应用程序列表中(点击它以展开列表)。进一步滚动 - 一直到最底部 - 你会发现不受监控的应用程序。这些是您特别希望从 App 电源监视器邪恶范围中排除(白名单)的应用程序。
在未监控的应用程序菜单中,您可以点击三点菜单从列表中添加或删除应用程序。您不必为此烦恼,只需完全关闭 App 电源监控功能,因为它对电池寿命几乎没有影响,只会妨碍 Galaxy 手机的正常运行。
这是过度的,在某些情况下是彻头彻尾的误导,当其他安卓设备没有它时,使用恐吓策略让你依赖三星的软件。
先试试这个..让我知道这之后是否还有任何问题。
如果您的代码在其他设备或模拟器上工作,这应该是问题所在。
推荐阅读
- postman - 如何获取 chrome 的默认客户端证书并在 postman 中使用?
- multithreading - 通过多线程使用 GPU 委托 v2 api 初始化多个 tflite 模型
- reactjs - nginx + 反向代理 + 反应路由器 - 路由不起作用
- javascript - 翻译 FullCalendar 标题
- saml - grails.plugin.springsecurity.saml.plugin 的错误甚至没有激活它在文件的运行时查看
- azure - Azure ADF 管道无法连接到 Azure SQL
- html - 无法使用环绕文本居中元素,因为文本环绕将父级设置为 100% 宽度
- mysql - 如何编写 SQL 查询来过滤一列有两个值的行?
- r - 如何从数据表中找到 ITT、ATT 和 LATE?
- mysql - 在 MySQL 表的列中插入数组 BigInt