【问题标题】:How to show a pop-up notification when the phone is locked?如何在手机锁定时显示弹出通知?
【发布时间】:2020-01-04 21:01:23
【问题描述】:

我想在锁定屏幕上将此通知显示为弹出窗口或警报对话框。

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

}

【问题讨论】:

    标签: android firebase popup android-notifications


    【解决方案1】:

    要控制锁定屏幕通知中可见的详细程度,请调用setVisibility() 并指定以下值之一:

    • VISIBILITY_PUBLIC 显示通知的完整内容。
    • VISIBILITY_SECRET 不会在锁定屏幕上显示此通知的任何部分。
    • VISIBILITY_PRIVATE 显示通知的图标和内容标题等基本信息,但隐藏了通知的全部内容。

    当设置VISIBILITY_PRIVATE 时,您还可以提供隐藏某些细节的通知内容的替代版本。例如,SMS 应用程序可能会显示一条通知,显示您有 3 条新短信,但会隐藏消息内容和发件人。要提供此替代通知,首先像往常一样使用 NotificationCompat.Builder 创建替代通知。然后使用setPublicVersion() 将替代通知附加到普通通知。

    但是,用户始终可以最终控制他们的通知是否在锁定屏幕上可见,甚至可以根据您应用的通知渠道进行控制。

    更多详情click here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      相关资源
      最近更新 更多