安卓如何在屏幕上显示通知

37

我一直在处理推送通知,我能够实现并在状态栏中显示它,但是我面临的问题是,我想即使手机被锁定也要显示它,在锁屏界面上有一个“滑动以解锁”的区域,我曾经看到过那样的通知,但找不到任何示例。

示例: 就像当你收到未接来电时,它会显示在你屏幕上的锁定按钮下方。

代码:

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

2
不相关的,你不一定需要将所有内容保存为本地变量。 - CrackerJack9
2
现在暂时不可能。 - Basic Coder
@Copa 你是什么意思?你的参考是什么?我有几个应用程序可以以OP描述的方式显示通知(通知栏,在屏幕锁定时)。 - CrackerJack9
4
目前无法在锁屏界面上显示通知、小部件或任何内容。如果您使用自定义锁屏应用,则可能可以实现,但并非一定如此。自定义锁屏应用程序是普通应用程序,因此可以工作。但 Android 自带的原始锁屏界面无法更改。我没有官方链接告诉您为什么无法更改,但目前没有 API 可以完成此项工作。 - Basic Coder
@Copa:Handcent SMS成功在锁屏界面上方弹出对话框,但我不知道它是如何实现的... - Jon Adams
5个回答

22
使用NotificationCompat.Builder创建通知
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher) // 通知图标
            .setContentTitle("通知标题") // 通知标题
            .setContentText("你好,世界") // 通知内容
            .setAutoCancel(true); // 点击后清除通知
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
这样做并不能在设备的锁屏界面上显示通知,对吗? - Jay Sidri
1
是的,即使设备被锁定,它也会显示通知。 - Naruto
4
Lint指出FLAG_ACTIVITY_NEW_TASK与PendingIntent不兼容。 - Amir Uval
1
R.mipmap.ic_launcher - kinkajou

6
使用NotificationCompat.Builder创建通知,但确保将可见性设置为public:
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

2
构建器已弃用。 - abbasalim

5

您尝试过使用标志创建alertdialog吗?flag_show_when_locked应该能解决问题。 请参考此线程,您可以在这里找到更详细的答案。 Android锁屏小部件


4
我通过在通知构建器中添加这一行代码来解决了这个问题。
builder.setOngoing(true);

它还将使通知不可由用户取消,但它解决了问题。
致谢:Marian Klühspies(link

2
您所看到的通知可能实际上是放置在自定义小部件主机锁定屏幕上的小部件。
如果您查看Android平台源代码中的InstallWidgetReceiver,最晚版本为4.4.3,如下所示:

https://android.googlesource.com/platform/packages/apps/Launcher3/+/master/src/com/android/launcher3/InstallWidgetReceiver.java

您会看到作者留下的这个注释:

/** * 我们可能稍后会拓展它,以便允许外部应用程序放置小部件,但目前, * 我们只想在其他地方检查时公开该操作。 */

您可以看到,与 InstallShortCutReceiver.java 不同,Google 实际上没有完善 InstallWidgetReceiver.java。因此,至少在 4.4.3 版本之前,您无法像使用 InstallShortCutReceiver 一样向本机锁定屏幕添加小部件。
除非您构建自己的锁屏应用作为小部件主机,并且用户安装了该应用代替原生锁屏,否则您可能无法使用小部件。
然而,另一种方法是仅使用设置 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 的活动。
这将显示您的活动,无论屏幕是否已锁定。当屏幕被锁定时关闭此活动将显示锁定屏幕。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接