如何显示紧凑的通知(如果可用)在用户设备上? [英] how to show compact notification if it is available on the user device?

查看:210
本文介绍了如何显示紧凑的通知(如果可用)在用户设备上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图展现一个紧凑的通知,像这样(的一个展开和折叠):

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)

首先该怎么做检查,如果是的setStyle可用电流设备和条件如果没有正常显示通知?

First how to do a condition that check if setStyle is available on current device and if not show normal notification?

二如何初始化mIcon2具有相同的图标,仅米康更大,因为它不带参数?

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

4是什么bigText字符的最大计数?

4th What is the maximum count of characters for bigText?

推荐答案

所以,这真的取决于你想要使用的具体细节,但在这里,我从工作中应用的例子。

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.

这个例子显示了一个BigPicture风格,但你可以从文档收件箱或BigText风格过于得到的。

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.

这篇关于如何显示紧凑的通知(如果可用)在用户设备上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆