如何显示紧凑通知(如果用户设备上可用)



>我正在尝试显示这样的紧凑通知(展开和折叠的通知):

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

我收到以下错误:

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)

首先如何执行检查setStyle在当前设备上是否可用的条件,如果不显示正常通知?

其次,如何将 mIcon2 初始化为具有与 mIcon 相同的图标,只是更大,因为它不需要 int?

构建后的第三个 如何实际触发通知显示? 和旧的

一样吗
// show the notification
mNotificationManager.notify(1, noti);

第四 大文本的最大字符数是多少?

因此,这实际上取决于您要使用的特定详细信息,但这里是我使用的应用程序的示例。

请注意,我只尝试加载要使用的图像,否则我会浪费内存和下载时间。

该示例显示了 BigPicture 样式,但您也可以从文档中获取收件箱或 BigText 样式。

// 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.
这是我

所做的:

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

最新更新