具有多行摘要的自定义Android通知BigPicture不适用于API 25和API 26

时间:2017-10-04 04:50:55

标签: java android push-notification push android-push-notification

我使用此类代码成功修改了大图片通知

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上,甚至没有调用这种方法。看不到它的解释

2 个答案:

答案 0 :(得分:1)

重写getStandardView后 - 您无法在API> = 24上访问此方法。但如果你打电话 createBigContentView,Android会调用getStandardView方法,你可以修改RemoteView。然后,您需要将接收到的RemoteView设置为自定义大内容视图。

   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 :(得分:-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中没有任何问题,并且经过确认测试。