phone 锁定时应用内通知不会显示

In app notification wont show when phone locked

我创建了一个应用内通知,该通知通过后台服务触发,由 firebase 值事件侦听器触发。

通知会在 phone 解锁时显示,即使应用程序在后台,但不会在 phone 锁定时显示。

    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());
}

除非您共享代码,否则不清楚您显示的是哪种通知。

如果是 heads-up 通知,这是预期的行为:

it appears only if the device is unlocked.

如果您正在测试的设备在 Android 低于 5.0 的版本上运行,它不会显示通知:

Beginning with Android 5.0, notifications can appear on the lock screen.

即使您可以将通知设置为显示在 lock screen 上:

You can programmatically set the level of detail visible in notifications posted by your app on a secure lock screen, or even whether the notification will show on the lock screen at all.

您必须检查 phone 上的设置,因为:

Users can use the system settings to choose the level of detail visible in lock screen notifications, including the option to disable all lock screen notifications. Starting with Android 8.0, users can choose to disable or enable lock screen notifications for each notification channel.

我能够解决这个问题,似乎 android OS 会终止后台服务以节省电池电量。 因为我是从应用程序内部而不是服务器发送通知,所以这是应该发生的,因此前台服务非常适合,因为它不会被系统杀死,因此我的问题解决了。

谢谢大家