简体   繁体   English

在锁定屏幕上显示通知

[英]Show notification on lock screen

I have made a notification following the documentations, https://developer.android.com/training/notify-user/build-notification#lockscreenNotification我已按照文档https://developer.android.com/training/notify-user/build-notification#lockscreenNotification发出通知

but when i try to show the notification with the screen locked with setVisiblity() it don't appear但是当我尝试在使用 setVisiblity() 锁定屏幕的情况下显示通知时,它不会出现

binding.btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Update Existing Notification and if dismissed new one will be made

                NotificationCompat.Builder builder2 = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
                        .setSmallIcon(R.drawable.notification_icon)
                        .setContentTitle("NEW textTitle")
                        .setContentText("NEW textContent")
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setAutoCancel(true)
                        .setContentIntent(pendingIntent)
                        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                        .setOnlyAlertOnce(true);

                notificationManager.notify(1, builder2.build());
            }
        });

everything else works fine其他一切正常

I do this and it works:我这样做并且有效:

Add this in Manifest:在清单中添加:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

Add this in Function:在 Function 中添加:

public static void getSynchronizeNotification(Context context, DataResponse dataResponse) {

        Bitmap Largeicon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(NOTIFICATION_SYNCHRONIZE_ID);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
        KeyguardManager km = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);
        final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("IN");
        kl.disableKeyguard();

        PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "SMOK Komunal");
        wl.acquire();
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                .setCategory(Notification.CATEGORY_PROMO)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("NEW textTitle")
                .setContentText("NEW textContent")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(buildNotificationAfterSynchronizeText(dataResponse)))
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setLargeIcon(Largeicon)
                .setContentIntent(pendingIntent)
                .setVibrate(vibratePattern)
                .setLights(Color.GREEN, 2000, 2000)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

        wl.release();

        notificationManager.notify(0, notificationBuilder.build());
    }

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

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