如何在用户设备上显示紧凑通知?

时间:2014-03-01 19:40:48

标签: android android-notifications

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

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?

构建后第3个如何实际触发通知显示?是不是像旧的一样

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

4 bigText的最大字符数是多少?

2 个答案:

答案 0 :(得分:2)

所以,这实际上取决于你想要使用的具体细节,但这里是我工作的应用程序中的一个例子。

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

该示例显示了BigPicture样式,但您也可以从文档中获取Inbox或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.

答案 1 :(得分:-1)

这就是我所做的:

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