简体   繁体   English

如何在手机锁定时显示弹出通知?

[英]How to show a pop-up notification when the phone is locked?

I want to show this notification as pop-up window or alert dialogue on the lock screen.我想在锁定屏幕上将此通知显示为弹出窗口或警报对话框。

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

}

To control the level of detail visible in the notification from the lock screen, call setVisibility() and specify one of the following values:要从锁定屏幕控制通知中可见的详细程度,请调用setVisibility()并指定以下值之一:

  • VISIBILITY_PUBLIC shows the notification's full content. VISIBILITY_PUBLIC显示通知的完整内容。
  • VISIBILITY_SECRET doesn't show any part of this notification on the lock screen. VISIBILITY_SECRET不会在锁定屏幕上显示此通知的任何部分。
  • VISIBILITY_PRIVATE shows basic information, such as the notification's icon and the content title, but hides the notification's full content. VISIBILITY_PRIVATE显示基本信息,例如通知的图标和内容标题,但隐藏通知的全部内容。

When VISIBILITY_PRIVATE is set, you can also provide an alternate version of the notification content which hides certain details.设置VISIBILITY_PRIVATE ,您还可以提供隐藏某些详细信息的通知内容的替代版本。 For example, an SMS app might display a notification that shows You have 3 new text messages but hides the message contents and senders.例如,短信应用可能会显示一条通知,显示您有 3 条新短信,但隐藏了消息内容和发件人。 To provide this alternative notification, first create the alternative notification with NotificationCompat.Builder as usual.要提供此替代通知,首先像往常一样使用 NotificationCompat.Builder 创建替代通知。 Then attach the alternative notification to the normal notification with setPublicVersion() .然后使用setPublicVersion()将替代通知附加到普通通知。

However, the user always has final control over whether their notifications are visible on the lock screen and can even control that based on your app's notification channels.但是,用户始终可以最终控制他们的通知是否在锁定屏幕上可见,甚至可以根据您应用的通知渠道进行控制。

For more detail click here有关更多详细信息, 请单击此处

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

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