Уведомление Android без R.java

Мне нужно показать локальное уведомление в Android. Но объект Notification Builder должен иметь любой значок, чтобы отобразить его в строке состояния. Имеет определенную обязательную функцию, такую ​​как setSmallIcon(int), Требуется целочисленный аргумент R.java файл. Но мне нужно дать прямой URL изображения без использования R.java файл. Код:

 NotificationCompat.Builder builder = new Builder(getContext());        
            Notification notification = builder.setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(title)
                    .setContentText(text)
                    .build();

Без R.drawable.ic_launcher. Как мне этого добиться?

2 ответа

Решение

Прежде всего, вы должны загрузить свое изображение: https://github.com/koush/ion

Затем:

Icon ic = Icon.createWithContentUri("uri of your downloaded image");  
builder.setSmallIcon(ic);

См. Следующий код

    private void sendNotification(Bundle extras) {
    Intent myIntent = null;
    Bundle bundle = extras;
    Set<String> set = bundle.keySet();
    for (String s : set) {
        Log.d("bundle", s);
    }
    String title = bundle.getString("title");
    String message = bundle.getString("detail_text");
    String imageUrl = bundle.getString("image");
    String url = bundle.getString("url");
    Log.d("url", title + " : " + imageUrl + " : " + url + " : " + bundle.getString("from") + " : " + bundle.getString("type"));
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent notificationIntent;
    if (url != null) {
        notificationIntent = new Intent(this, SplashScreenActivity.class);
        notificationIntent.putExtra("url", url);
        notificationIntent.putExtra("title", title);
        notificationIntent.setAction("FROM_NOTIFICATION");
    } else {
        notificationIntent = new Intent(this, SplashScreenActivity.class);
        notificationIntent.setAction("FROM_NONE_NOTIFICATION");
    }
    LogUtils.logD(title + " :  " + url);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setSmallIcon(R.drawable.small_notification);
   mBuilder.setLargeIcon(BitmapFactory.decodeResource(getBitmapFromURL("YOUR URL"));

  /*  if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
     //   mBuilder.setColor(this.getColor(R.color.black));
    }*/
    final int version = Build.VERSION.SDK_INT;
    if (version >= 23) {
        mBuilder.setColor(ContextCompat.getColor(this, R.color.black));
    } else {
        mBuilder.setColor(this.getResources().getColor(R.color.black));
    }
    mBuilder.setTicker("your title");
    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    mBuilder.setSound(uri);
    mBuilder.setPriority(Notification.PRIORITY_MAX);
    // mBuilder.setV
    mBuilder.setContentTitle(title);
    mBuilder.setContentText(message);
    NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle();
    if (imageUrl != null) {
        s.bigPicture(getBitmapFromURL(imageUrl));
        s.setSummaryText(message);
        mBuilder.setStyle(s);
    } else {
        mBuilder.setContentText(message);
    }
    mBuilder.setAutoCancel(true);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}


public Bitmap getBitmapFromURL(String strURL) {
    try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

Небольшая иконка обязательна, я пытался удалить эту иконку, но мое приложение зависло ** ** Ссылка

 mBuilder.setLargeIcon(BitmapFactory.decodeResource(getBitmapFromURL("YOUR URL"));
Другие вопросы по тегам