【问题标题】:Lock screen notification text锁屏通知文字
【发布时间】:2016-02-22 05:02:18
【问题描述】:

我正在构建简单的 android 应用程序。 我在通知方面遇到问题。当应用程序收到通知时,它会正确显示所有图片和文本,而在锁定屏幕上,如果手机被锁定,则不会显示文本。要查看文本,我必须向下滑动通知。

这是代码:

NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
            bigText.bigText(body);

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_stat_luceterna_notifica_90)
                    .setContentTitle("Luceterna")
                    .setLargeIcon((((BitmapDrawable) this.getResources().getDrawable(R.mipmap.ic_launcher_lux)).getBitmap()))
                    .setAutoCancel(true)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

            mBuilder.setStyle(bigText);

            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            mBuilder.setSound(alarmSound);

            NotificationManager mNotificationManager = (NotificationManager) context
           .getSystemService(Context.NOTIFICATION_SERVICE);

请任何人帮助我吗?我找不到任何问题的答案。

提前致谢。

【问题讨论】:

  • 对不起,我忘记了最后一行代码:mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());

标签: android notifications android-5.1.1-lollipop


【解决方案1】:

我知道你问这个问题已经有一段时间了,但我今天遇到了同样的问题,并找到了解决方案。

要解决这个问题,你必须重写 NotificationCompat setContentTitlesetContentDescritption

使用 BigTextStyle 的:setBigContentTitlebigText 你必须同时使用它们,里面有相同的文本。因此,您的代码如下所示:

String title = "Title";
String description = "Description";

NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(title);
bigTextStyle.bigText(description);

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setContentTitle(title)
            .setContentText(description)
            .setSmallIcon(R.drawable.ic_launcher)
            .setStyle(bigTextStyle);


NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());

【讨论】:

    【解决方案2】:
    NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher) // notification icon
                .setContentTitle("Notification!") // title for notification
                .setContentText("Hello word") // message for notification
                .setAutoCancel(true); // clear notification after click
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
    mBuilder.setContentIntent(pi);
    NotificationManager mNotificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());
    

    一个帮助你的例子

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多