简体   繁体   English

手机锁定时,应用内通知不会显示

[英]In app notification wont show when phone locked

I have created an in app notification that gets fired with a background service, which is triggered by firebase value event listener. 我创建了一个应用内通知,该通知被后台服务触发,该后台服务由firebase值事件侦听器触发。

The notification shows when the phone is unlocked even if the app is in background but wont show when phone is locked. 通知会显示手机何时被解锁,即使该应用程序处于后台,但手机被锁定时也不会显示。

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

Unless you share code, it's unclear which kind of notification you are displaying. 除非共享代码,否则不清楚显示的是哪种通知。

If it's a heads-up notification, this is the expected behaviour: 如果是平视通知,这是预期的行为:

it appears only if the device is unlocked. 仅在设备解锁后才会显示。

If the device you are testing with runs on Android version below 5.0, it won't show the notification: 如果您要测试的设备在低于5.0的Android版本上运行,则不会显示通知:

Beginning with Android 5.0, notifications can appear on the lock screen. 从Android 5.0开始,通知可以显示在锁定屏幕上。

And even though you can set your notification to be displayed on the 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. 您可以通过程序设置在安全锁定屏幕上由应用发布的通知中可见的详细程度,甚至可以设置是否将通知显示在锁定屏幕上。

You would have to check the settings on the phone, because: 您必须检查手机上的设置,因为:

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

I was able to work around the problem, it seemed that android OS would kill services in background for battery saving. 我能够解决该问题,似乎android操作系统会在后台终止服务以节省电池。 AS i was sending notifications from within the app and not the server this was supposed to happen therefore a foreground service was a perfect fit as it would not be killed by the system and hence my problem solved. 由于我是从应用程序而不是服务器中发送通知,因此这应该发生,因此前台服务非常适合,因为它不会被系统杀死,因此我的问题得以解决。

Thanks everyone. 感谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM