【问题标题】:In app notification wont show when phone locked手机锁定时应用内通知不会显示
【发布时间】:2019-07-01 19:55:20
【问题描述】:

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

【问题讨论】:

  • 显示您到目前为止所做的代码,以便我们更好地帮助您
  • 添加了通知医生我调用通知的代码
  • 请添加代码以显示通知
  • 刚刚做了,notifydoctor()函数有条件检查版本,因为oreo有不同的通知,调用sendnotifciation()函数

标签: android android-notifications


【解决方案1】:

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

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

只有在设备解锁时才会出现。

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

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

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

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

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

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

【讨论】:

【解决方案2】:

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

谢谢大家。

【讨论】:

  • 但是当应用程序处于终止状态并且手机被锁定 10 分钟时,也不会触发通知。但是一旦手机解锁,就会触发通知。我也在使用前台服务。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多