自定义Android通知的BigPicture和多行摘要在API 25和API 26上无法正常工作。

3

我成功地使用以下代码修改了大图通知

static class MyMultiLineBigPictureStyle extends Notification.BigPictureStyle{
    @NonNull
    @Override
    protected RemoteViews getStandardView(int layoutId) {
        RemoteViews ret =  super.getStandardView(layoutId);
        int id =  Resources.getSystem().getIdentifier("text", "id", "android");
        ret.setBoolean(id, "setSingleLine", false);
        ret.setInt(id, "setLines", 4);
        return ret;
    }
}

这在 API < 24 上运行良好。但在 API 24、25 和 26 上,甚至都没有调用该方法。无法找到解释。


为什么不将处理BigPictureStyle的代码移动到通知处理区域,而不是依赖于覆盖BigPictureStyle回调函数? - Sam
你的意思是通过NotificationBuilder(那里没有“lines”设置)还是自定义layout.xml?使用自定义布局通知看起来与系统原生通知不同。我不认为有简单的方法可以支持所有这些差异。 - Enuviel
2个回答

1
在重写了getStandardView方法后,您无法在API>=24上调用此方法。但是,如果您调用createBigContentView,则Android会调用getStandardView方法,并且您可以获得修改后的RemoteView。然后,您需要将收到的RemoteView设置为自定义“big content view”。
   if (Build.VERSION.SDK_INT >= 24) {
    try {
      RemoteViews remoteView = notificationBuilder.createBigContentView();
      if (remoteView != null) {
        notificationBuilder.setCustomBigContentView(remoteView);
      }
    } catch (Throwable t) {
      Log.e("","Cannot modify push notification layout.");
    }
  }

-1

好的,由于我无法在评论中回复您,因此将其作为答案发布在这里。

public class SendNotificationAsyncTask extends AsyncTask<String, Void, Bitmap> {

    /*///////////////////////////////////////////////////////////////
    // MEMBERS
    *////////////////////////////////////////////////////////////////
    private static final String TAG = Globals.SEARCH_STRING + SendNotificationAsyncTask.class.getSimpleName();
    private Context mContext;
    private String mMessage;
    private String mImageUrl;
    private String mIdOfDetailToShow;


    /*///////////////////////////////////////////////////////////////
    // CONSTRUCTOR
    *////////////////////////////////////////////////////////////////
    public SendNotificationAsyncTask(Context context, String imageUrl, String message, String idToShow) {
        super();
        mContext = context;
        mMessage = message;
        mImageUrl = imageUrl;
        mIdOfDetailToShow = idToShow;

    }


    /*///////////////////////////////////////////////////////////////
    // BASECLASS OVERRIDES
    *////////////////////////////////////////////////////////////////
    @Override
    protected Bitmap doInBackground(String... params) {
        InputStream in;
        try {
            URL url = new URL(mImageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(10000);
            connection.setReadTimeout(10000);
            connection.setDoInput(true);
            connection.setInstanceFollowRedirects(true);
            connection.connect();
            in = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(in);

            return myBitmap;

        } catch (MalformedURLException e) {
            A35Log.e(TAG, e.getMessage());

        } catch (IOException e) {
            A35Log.e(TAG, e.getMessage());

        }

        return null;

    }
    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);

        try {
            NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

            Intent intent = new Intent(mContext, SplashScreenActivity.class);
            intent.putExtra(Globals.INTENT_KEYS.KEY_FROM_BADGE_ACCESS, true);
            intent.putExtra(Globals.INTENT_KEYS.KEY_ID_OF_DETAIL_TO_OPEN, mIdOfDetailToShow);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT);

            Notification notification = new Notification.Builder(mContext)
                    .setContentTitle(mContext.getResources().getString(R.string.app_name))
                    .setContentText(mMessage)
                    .setSmallIcon(R.drawable.logo_main_white)
                    .setContentIntent(pendingIntent)
                    .setStyle(new Notification.BigPictureStyle().bigPicture(result))
                    .setLargeIcon(result).build();

            // hide the notification after its selected
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(1, notification);

        } catch (Exception e) {
            A35Log.e(TAG, e.getMessage());

        }

    }

}

我基本上下载图像,然后创建大图像通知。在25或26中,我没有遇到任何问题,并且已经确认测试过了。


好的,如何强制它显示两行或更多文本摘要,就像这样: https://stackoverflow.com/questions/28043878/android-big-picture-style-notification-with-multi-line-summary-text - Enuviel
我实际上并没有尝试过那样做。我还没有需要处理两行的情况,但一旦你想出解决方案,请分享回来。 - Sam
一种取巧的方法是将文本包含在图像中,并从下载的位图和要显示的文本创建一个单一的图像。虽然不完美,但你肯定可以做到。 - Sam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接