【问题标题】:How can I show notification on lock screen?如何在锁定屏幕上显示通知?
【发布时间】:2020-11-25 21:54:29
【问题描述】:

我已根据文件发出通知, https://developer.android.com/training/notify-user/build-notification#lockscreenNotification

但是当我尝试在使用 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());
            }
        });

其他一切正常

【问题讨论】:

    标签: android notifications lockscreen


    【解决方案1】:

    我这样做了,它有效:

    在清单中添加:

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

    在函数中添加这个:

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

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-30
      • 2020-06-17
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多