3

上では動作しませんカスタムAndroidの通知BigPictureが、私は成功し、このようなコードを使用して大きな画像通知を変更API 25およびAPI 26

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,25でもAPI < 24上で動作し、このメソッドは呼び出されていません。それについての説明が表示されません

+0

BigPictureStyleコールバックをオーバーライドするのではなく、BigPictureStyleを処理するコードを通知処理エリアに移動するだけでいいのですか? – Sam

+0

NotificationBuilderを使用していることを意味します(そこには「行」設定はありません)か、custom layout.xmlはありますか?カスタムレイアウト通知では、システムネイティブ通知とは異なります。カスタムレイアウトでこれらの違いをすべてサポートする簡単な方法はありません。 – Enuviel

答えて

-1

[OK]私はあなたにこれを投稿することはできませんので、ここに回答として投稿してください。

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で何の問題もなく、テスト済みであることが確認されています。

+0

[OK]をクリックし、2行以上のテキストをまとめて表示するにはどうすればよいでしょうか: https://stackoverflow.com/questions/28043878/android-big-picture-style-notification-with-multi-line-要約 - テキスト – Enuviel

+0

私は実際にそれをやろうとしていません。私はまだ2行を行う必要はありませんでしたが、一度それを理解したら、あなたの解決策を共有してください – Sam

+0

画像の中にテキストを入れて、ダウンロードしたビットマップと必要なテキスト表示する。それは完璧ではありませんが、確かにそれを行うことができます。 – Sam

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."); 
    } 
    } 
関連する問題