Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

Heads-up Notification - 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());

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 examples are not available. does anyone know how to do it?

I made it working by adding:

.setDefaults(Notification.DEFAULT_VIBRATE)

is this the best way?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

EDIT:

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

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: 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...