android - 如何在锁定屏幕上显示通知?
问题描述
我已经按照文档发出通知, 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());
}
});
其他一切正常
解决方案
我这样做并且它有效:
在清单中添加:
<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());
}
推荐阅读
- matlab - 在 MATLAB 中从许多相同大小的矩阵中获取相同的子集
- javascript - 为什么 Webpack 认为我正在运行开发构建?
- c++ - 为什么我得到“候选构造函数不可行”?
- html - 在移动视图中引导 4 张单独的卡片?
- java - JADE IPMS:代理迁移失败
- swift - 无法以编程方式将情节提要中的颜色集更改为 xcassets 目录中的颜色
- python - 使用 f String 插入字符或符号@
- c++ - 如何在 C++ arduino 中从字符串中拆分特定部分
- opendaylight - 铍发布无法在 Web 界面上登录 (http://10.10.10.2:8181/index.html)
- angular - Angular 7 - 未捕获(承诺):错误:StaticInjectorError(AppModule)[BASE_URL]