Android 如何在屏幕上显示通知

发布于 2024-12-05 02:17:19 字数 1010 浏览 2 评论 0原文

我一直在研究推送通知,我能够实现它并将其显示在状态栏上,我面临的问题是即使手机锁定,我也想显示它,在锁定屏幕下显示(“拖动解锁”),我见过类似的通知,但找不到任何示例。

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

代码:

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

I've been working on push notifications and I am able to implement it and display it on status bar, the problem I am facing is that I want to display it even if the phone is lock, Under the lock screen where it says ("drag to unlock"), I have seen notifications like that but cant find any example to that.

Example:
Just like when you received a missed call , it will show it under the lock button on your screen.

Code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

追星践月 2024-12-12 02:17:20

使用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

Create Notification using NotificationCompat.Builder but make sure to put visibility to public like

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
愿得七秒忆 2024-12-12 02:17:20

您是否尝试过使用标志创建警报对话框? flag_show_when_locked 应该可以解决问题。
请参考这个帖子,您应该在这里找到更详细的答案。
Android 锁屏小部件

Have you tried creating the alertdialog with a flag? The flag_show_when_locked should do the trick.
Please refer to this thread, you should find a more detailed answer here.
Android Lock Screen Widget

水中月 2024-12-12 02:17:20

我通过将此行添加到通知生成器来修复此问题,

builder.setOngoing(true);

它还会使用户无法取消通知,但它解决了问题。

致谢:Marian Klühspies(链接

I fixed this by adding this line to notification builder

builder.setOngoing(true);

It will also make notification not cancelable by user, but it solves the problem.

Credits to: Marian Klühspies (link)

感情废物 2024-12-12 02:17:20

您看到的通知实际上可能是放置在自定义小部件主机锁屏上的小部件。

如果您在此处查看晚至 4.4.3 的 InstallWidgetReceiver 的 android 平台源代码:

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

您将看到此注释作者:

/**
* 我们可能稍后会充实这一点,以处理允许外部应用程序放置小部件的问题,但现在,
* 我们只是想公开周围的操作以便在其他地方检查。
*/

你可以看到,InstallWidgetReceiver.java 实际上并没有像 InstallShortCutReceiver.java 那样被 google 充实。因此,至少在 4.4.3 之前,您无法像使用 InstallShortCutReceiver 添加快捷方式到主屏幕那样将小部件添加到本机锁定屏幕。

除非您构建自己的锁屏应用程序作为小部件主机,并且用户安装代替本机,否则您可能无法使用小部件。

然而,另一种方法是只设置一个活动 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

无论屏幕是否锁定,这都会显示您的活动。屏幕锁定时关闭此活动将显示锁定屏幕。

The notifications you have seen may actually be widgets placed on a custom widget host lockscreen.

If you look at the android platform source code for InstallWidgetReceiver as late as 4.4.3 here:

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

You will see this note by the author:

/**
* We will likely flesh this out later, to handle allow external apps to place widgets, but for now,
* we just want to expose the action around for checking elsewhere.
*/

And you can see that InstallWidgetReceiver.java is in fact not fleshed out by google in the same way as InstallShortCutReceiver.java is. So it seems at least up to 4.4.3 you cant add widgets to the native lock screen in the same way that you can for example add a shortcut to the homescreen using InstallShortCutReceiver.

Unless you build your own lockscreen app as a widget host and the user installs in lieu of the native you may be out of luck using a widget.

Another approach however is to just us an activity that sets getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

This will display your activity whether the screen is locked or not. Dismissing this activity when the screen is locked will display the locked screen.

感性 2024-12-12 02:17:19

使用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-通知/

Create Notification using 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());

Push Notification on locked Screen
http://www.hongkiat.com/blog/android-lock-screen-notifications/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文