在应用程序中,通知不会显示电话锁定时



我创建了一个由背景服务触发的应用程序通知,该通知是由Firebase Value Event Everter触发的。

通知显示了何时解锁手机,即使该应用在后台,但不会显示手机何时锁定。

    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开始,用户可以选择为每个通知频道禁用或启用锁定屏幕通知。

我能够解决这个问题,似乎Android OS会在节省电池的背景下杀死服务。当我从应用程序中发送通知而不是应该发生的服务器时,前景服务是一种完美的拟合,因为它不会被系统杀死,因此我的问题解决了。

谢谢大家。

最新更新