Android 如何在屏幕上显示通知
我一直在研究推送通知,我能够实现它并将其显示在状态栏上,我面临的问题是即使手机锁定,我也想显示它,在锁定屏幕下显示(“拖动解锁”),我见过类似的通知,但找不到任何示例。
示例: 就像您收到未接来电一样,它会显示在屏幕上的锁定按钮下方。
代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用NotificationCompat.Builder创建通知,但确保将可见性公开,例如
Create Notification using NotificationCompat.Builder but make sure to put visibility to public like
您是否尝试过使用标志创建警报对话框? 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
我通过将此行添加到通知生成器来修复此问题,
它还会使用户无法取消通知,但它解决了问题。
致谢:Marian Klühspies(链接)
I fixed this by adding this line to notification builder
It will also make notification not cancelable by user, but it solves the problem.
Credits to: Marian Klühspies (link)
您看到的通知实际上可能是放置在自定义小部件主机锁屏上的小部件。
如果您在此处查看晚至 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:
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.
使用NotificationCompat.Builder创建通知
<代码>
在锁定屏幕上推送通知
http://www.hongkiat.com/blog/android-lock-screen-通知/
Create Notification using NotificationCompat.Builder
Push Notification on locked Screen
http://www.hongkiat.com/blog/android-lock-screen-notifications/