Android如何在屏幕上显示通知

use*_*669 28 notifications android lockscreen

我一直致力于推送通知,我能够实现它并在状态栏上显示它,我面临的问题是我想显示它,即使手机是锁定的,在它所说的锁定屏幕下("拖动解锁"),我已经看过这样的通知,但无法找到任何示例.

示例: 就像您收到未接来电一样,它会在屏幕上的锁定按钮下显示.

码:

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon_launcher;
CharSequence tickerText = "MyApplication";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE|Notification.DEFAULT_LIGHTS;;
CharSequence contentTitle = this.title;
CharSequence contentText = this.message;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTICE_ID, notification);
Run Code Online (Sandbox Code Playgroud)

Kir*_*ela 17

使用NotificationCompat.Builder创建通知

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

锁定屏幕上的推送通知 http://www.hongkiat.com/blog/android-lock-screen-notifications/

  • 当涉及到在设备的锁定屏幕上实际显示通知时,这会不会有帮助吗? (4认同)
  • Lint说FLAG_ACTIVITY_NEW_TASK与PendingIntent不兼容 (2认同)

Moh*_*our 6

使用 NotificationCompat.Builder 创建通知,但请确保向公众公开,例如

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder
        .setContentTitle("Title")
        .setContentText("content")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);//to show content in lock screen
Run Code Online (Sandbox Code Playgroud)

  • 建造者被弃用 (2认同)

Vir*_*igy 5

您是否尝试使用标志创建alertdialog?flag_show_when_locked应该可以解决问题.请参考这个主题,你应该在这里找到更详细的答案. Android锁屏小工具