我想知道如何只在通知栏和通知布局上的不同文字上写通知文字?
答案 0 :(得分:11)
接受的答案包含deprecated
类和方法。 Look at here
以下是使用NotificationCompat.Builder
构建Notification
s:
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.my_drawable_resource)
.setContentTitle("my title")
.setContentText("my content")
.setContentIntent(pendingIntent);
Notification notification = mBuilder.build();
// default phone settings for notifications
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
// cancel notification after click
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// show scrolling text on status bar when notification arrives
notification.tickerText = title + "\n" + content;
// notifiy the notification using NotificationManager
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
可以帮助新人!
答案 1 :(得分:9)
NotificationCompat.Builder
类的方法setTicker()
允许您这样做。
答案 2 :(得分:4)
int icon = R.drawable.notification_icon; // icon from resources
CharSequence tickerText = "Hello"; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = "My notification"; // expanded message title
CharSequence contentText = "Hello World!"; // expanded message text
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
你应该看看这个链接......... Click Me
答案 3 :(得分:1)
通知栏中显示的第一个文本由setTicker()方法定义。
这是一个例子:
private void showCustomNotification(){
final int NOTIFICATION_ID = 1;
String ns = Context.NOTIFICATION_SERVICE;
final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.mipmap.ic_launcher;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, getString(R.string.app_name), when);
notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; // Sound
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Java (programming language)")
.setTicker("Java (Open to see the info).")
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentText("Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented,[14] and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers \"write once, run anywhere\" (WORA),[15] meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.");
// NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}