单挑通知 - Android Lollipop [英] Heads-up Notification - Android Lollipop

查看:24
本文介绍了单挑通知 - Android Lollipop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示通知类型的提示,但我不能.我试过的

I'm trying to show a notification-type heads-up but I could not. What I tried

final Notification.Builder notif = new Builder(getApplicationContext())
    .setContentTitle(getString(R.string.title))
    .setContentText(getString(R.string.text))
//  .setTicker(getString(R.string.tick)) removed, seems to not show at all
//  .setWhen(System.currentTimeMillis()) removed, match default
//  .setContentIntent(contentIntent) removed, I don't neet it
    .setColor(Color.parseColor(getString(R.color.yellow))) //ok
    .setSmallIcon(R.drawable.ic_small) //ok
    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
//  .setCategory(Notification.CATEGORY_CALL) does not seem to make a difference
    .setPriority(Notification.PRIORITY_MAX); //does not seem to make a difference
//  .setVisibility(Notification.VISIBILITY_PRIVATE); //does not seem to make a difference

mNotificationManager.notify(Constants.NOTIFICATION_ID, notif.build());

通知仅显示为栏中的图标.我在 API 21 模拟器上使用 API 21(不是 L 预览版)我试过:
android:Theme.Holo.NoActionBar,
android:Theme.Holo.NoActionBar.Fullscreen
NotificationCompat.Builder

The notification is shown only as an icon in the bar. I'm using API 21 on API 21 emulator (not L preview) I have tried:
android:Theme.Holo.NoActionBar,
android:Theme.Holo.NoActionBar.Fullscreen
and NotificationCompat.Builder

SDK 示例不可用.有人知道怎么做吗?

SDK examples are not available. does anyone know how to do it?

我通过添加:

.setDefaults(Notification.DEFAULT_VIBRATE)

这是最好的方法吗?

推荐答案

根据 通知,您需要设置振动或铃声才能使单挑工作正常进行.但是,这里有一个不需要 VIBRATE 权限即可生成抬头通知的快速技巧:

According to Notifications, you are required to set a vibrate or ringtone to make Heads-up work. However, here's a quick hack that doesn't require VIBRATE permission to produce a head-up notification:

notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) notificationBuilder.setVibrate(new long[0]);

不要滥用单挑通知.请参阅此处了解何时使用提示通知:

Don't abuse heads-up notification. See here for when to use heads-up notification:

MAX:用于提醒用户注意时间紧迫或需要在他们继续执行特定任务之前解决的情况的关键和紧急通知.

MAX: For critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before they can continue with a particular task.

HIGH:主要用于重要的通信,例如包含用户特别感兴趣的内容的消息或聊天事件.高优先级通知触发提醒通知显示.

HIGH: Primarily for important communication, such as messages or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.

这篇关于单挑通知 - Android Lollipop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆