问题
I am trying to show a compact notification like so (the one that expand and collapse):
mIcon = R.drawable.ic_launcher;
Notification noti = new Notification.Builder(mContext)
.setContentTitle(mContentTitle)
.setContentText(mContentText)
.setSmallIcon(mIcon)
.setLargeIcon(mIcon2)
.setStyle(new Notification.BigTextStyle()
.bigText(mContentText))
.build();
I am getting the following errors:
Call requires API level 16 (current min is 11): android.app.Notification.Builder#setStyle
The method setLargeIcon(Bitmap) in the type Notification.Builder is not applicable for the arguments (int)
First how to do a condition that check if setStyle is available on current device and if not show normal notification?
Second how to initialize the mIcon2 to have the same icon as mIcon only bigger since it doesn't take int?
3rd after the build how to actually trigger the notification to show up? is it the same as the old one like so
// show the notification
mNotificationManager.notify(1, noti);
4th What is the maximum count of characters for bigText?
回答1:
so, it really depends on the specific details you want to use, but here an examples from the app I work at.
Note that I only try to load the image if it will be used, or else I'm wasting memory and download time.
That example shows a BigPicture style, but you can get from the docs the Inbox or BigText styles too.
// here is the base of a notification
NotificationCompat.Builder b = new NotificationCompat.Builder(context);
b.setSmallIcon(R.drawable.actionbar_icon);
b.setContentTitle(title);
b.setContentText(Html.fromHtml(msg));
b.setTicker(title);
b.setWhen(System.currentTimeMillis());
// notification shade open icon
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && url != null)
try {
b.setLargeIcon(Picasso.with(context).load(url)
.resizeDimen(android.R.dimen.notification_large_icon_width,
android.R.dimen.notification_large_icon_width).centerCrop().get());
} catch (IOException e) {
Log.e(this, "Failed to setLargeIcon", e);
}
NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle();
s.setSummaryText(Html.fromHtml(msg));
// expanded notification icon
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
if (expandedIconUrl != null) {
try {
s.bigLargeIcon(Picasso.with(context).load(expandedIconUrl).get());
} catch (IOException e) {
Log.e(this, "Failed to bigLargeIcon", e);
}
} else if (expandedIconResId > 0) {
s.bigLargeIcon(BitmapFactory.decodeResource(context.getResources(), expandedIconResId));
}
// background photo
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
try {
s.bigPicture(Picasso.with(context).load(bigImageUrl).get());
} catch (IOException e) {
Log.e(this, "Failed to bigPicture", e);
}
b.setStyle(s);
b.setContentIntent( /* add here your pending intent */ );
// here you can add up to 3 buttons to your notification
b.addAction(R.drawable.ic_notification_photo,
context.getString(R.string.notificationAction_viewPhoto),
/* and here the button onClick pending intent */);
Notification n = b.build();
// now just show it with Notify.
回答2:
this is what i have done:
public void createNotification() {
if (android.os.Build.VERSION.SDK_INT >= 11) {
createNotificationAPI11();
} else {
createNotificationOtherAPI();
}
}
private void createNotificationOtherAPI() {
// normal old notification
...
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void createNotificationAPI11() {
// new compact notification
....
}
来源:https://stackoverflow.com/questions/22119374/how-to-show-compact-notification-if-it-is-available-on-the-user-device