android - 手机锁定时应用内通知不会显示
问题描述
我创建了一个应用内通知,该通知由后台服务触发,该服务由 firebase 值事件侦听器触发。
即使应用程序在后台,通知也会在手机解锁时显示,但在手机锁定时不会显示。
public int onStartCommand(Intent intent, int flags, int startId) {
flag = true;
queueList = new ArrayList<>();
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser != null) {
reference = FirebaseDatabase.getInstance().getReference("queue").child(firebaseUser.getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
queueList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
try {
ChatQueue chatQueue = snapshot.getValue(ChatQueue.class);
if (chatQueue.getStatus().equals("pending")) {
queueList.add(chatQueue);
}
} catch (NullPointerException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(e);
}
}
if (count > 0) {
if (queueList.size() > 0) {
notifyDoctor();
ShortcutBadger.applyCount(getApplicationContext(), queueList.size());
} else {
ShortcutBadger.removeCount(getApplicationContext());
}
}
count++;
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
return START_NOT_STICKY;
}
private void sendNotification() {
int j = 220;
Intent intent = new Intent(this, ChatHomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, j, intent, PendingIntent.FLAG_ONE_SHOT);
final Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notif_icon)
.setContentTitle("Patient Alert!")
.setContentText("You have a new patient.")
.setAutoCancel(true)
.setSound(defaultSound).setContentIntent(pendingIntent);
final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
builder.setPriority(Notification.PRIORITY_MAX);
notificationManager.notify(j, builder.build());
}
解决方案
除非您共享代码,否则不清楚您正在显示哪种通知。
如果是单挑通知,这是预期的行为:
它仅在设备解锁时出现。
如果您正在测试的设备在低于 5.0 的 Android 版本上运行,则不会显示通知:
从 Android 5.0 开始,通知可以显示在锁定屏幕上。
即使您可以将通知设置为在锁定屏幕上显示:
您可以以编程方式设置应用在安全锁定屏幕上发布的通知中可见的详细程度,甚至可以设置通知是否显示在锁定屏幕上。
您必须检查手机上的设置,因为:
用户可以使用系统设置来选择锁定屏幕通知中可见的详细程度,包括禁用所有锁定屏幕通知的选项。从 Android 8.0 开始,用户可以为每个通知渠道选择禁用或启用锁屏通知。
推荐阅读
- apache-kafka - kafka客户端可以选择特定分区消费吗?
- javascript - Javascript - 排除特定表单标签之外的所有内容
- html - 如何设置数据错误的字体系列
- eclipse - Java(Mac OS):从文件中重新读取对象时,将我的对象写入文件会导致测试出现乱码
- asp.net-mvc - 将一个文件夹移回服务器 MVC
- wordpress - 如何仅在索引页面上显示弹出窗口 Wordpress
- jquery - 如何获取选择器之外的选项卡列表类。我正在使用作为我的这个元素
- beautifulsoup - 从棒球参考中解析 bs4 表
- bash - bash 表达式的增量值
- mongodb - 在 Meteor/MongoDB 中删除数组对象