[英]How to show a pop-up notification when the phone is locked?
我想在锁定屏幕上将此通知显示为弹出窗口或警报对话框。
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
Map<String, String> extraData = remoteMessage.getData();
String brandID = extraData.get("brandID");
String category = extraData.get("category");
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "TAC")
.setContentText(body)
.setContentTitle(title)
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_ALL) // must requires VIBRATE permission
.setPriority(NotificationCompat.PRIORITY_HIGH) //must give priority to High, Max which will considered as heads-up notification
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_launcher_background);
Intent intent;
if(category.equals("shoes")){
intent = new Intent(this, ReceiveNotification.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
else {
intent = new Intent(this, ReceiveNotification.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
intent.putExtra("brandID", brandID);
intent.putExtra("category", category);
PendingIntent pendingIntent
= PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel("TAC", "demo", NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(notificationChannel);
}
int id = (int) System.currentTimeMillis();
manager.notify(id, builder.build());
}
要从锁定屏幕控制通知中可见的详细程度,请调用setVisibility()
并指定以下值之一:
VISIBILITY_PUBLIC
显示通知的完整内容。VISIBILITY_SECRET
不会在锁定屏幕上显示此通知的任何部分。VISIBILITY_PRIVATE
显示基本信息,例如通知的图标和内容标题,但隐藏通知的全部内容。 设置VISIBILITY_PRIVATE
,您还可以提供隐藏某些详细信息的通知内容的替代版本。 例如,短信应用可能会显示一条通知,显示您有 3 条新短信,但隐藏了消息内容和发件人。 要提供此替代通知,首先像往常一样使用 NotificationCompat.Builder 创建替代通知。 然后使用setPublicVersion()
将替代通知附加到普通通知。
但是,用户始终可以最终控制他们的通知是否在锁定屏幕上可见,甚至可以根据您应用的通知渠道进行控制。
有关更多详细信息, 请单击此处
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.