【问题标题】:Show notifications on Screen in Android在 Android 的屏幕上显示通知
【发布时间】:2016-06-29 21:01:28
【问题描述】:

我正在使用此代码发送本地通知

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

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                i, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
                        .setContentTitle(title)
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(msg))
                        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify((int)value, mBuilder.build());

但是,如果手机屏幕被滑动锁定,它们会显示在状态栏上。我想在屏幕顶部显示推送通知,例如 whatsapp、facebook、短信等。

我怎样才能做到这一点?

【问题讨论】:

  • 我认为 Android OS 会自动处理它
  • @HirenPatel 好吧,不,我只在状态栏上看到小图标,而不是在锁定屏幕顶部显示
  • 默认情况下仅在 android 5 (lolipop) 支持抬头通知。因此,如果您仍然想这样做,那么您可以构建自定义通知视图并使用 windowmanager 服务将其添加到屏幕中。

标签: java android eclipse android-studio


【解决方案1】:

你可以这样做:

通过添加以下行:

setPriority(Notification.PRIORITY_MAX)

完整代码

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

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                i, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
                        .setContentTitle(title)
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(msg))
                        .setContentText(msg)
                        .setPriority(Notification.PRIORITY_MAX);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify((int)value, mBuilder.build());

希望这会对你有所帮助。

【讨论】:

  • 似乎没有工作我只收到顶级通知和通知
  • 它适用于我,但在棉花糖及以上,谢谢@Hiren
猜你喜欢
  • 2011-11-18
  • 1970-01-01
  • 1970-01-01
  • 2020-06-17
  • 2021-03-26
  • 1970-01-01
  • 2022-08-05
  • 2016-02-17
  • 1970-01-01
相关资源
最近更新 更多