首页 > 解决方案 > 手机锁定时应用内通知不会显示

问题描述

我创建了一个应用内通知,该通知由后台服务触发,该服务由 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());
}

标签: androidandroid-notifications

解决方案


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

如果是单挑通知,这是预期的行为:

它仅在设备解锁时出现。

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

从 Android 5.0 开始,通知可以显示在锁定屏幕上。

即使您可以将通知设置为在锁定屏幕上显示

您可以以编程方式设置应用在安全锁定屏幕上发布的通知中可见的详细程度,甚至可以设置通知是否显示在锁定屏幕上。

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

用户可以使用系统设置来选择锁定屏幕通知中可见的详细程度,包括禁用所有锁定屏幕通知的选项。从 Android 8.0 开始,用户可以为每个通知渠道选择禁用或启用锁屏通知。


推荐阅读