2

I need to show local notification in Android. But Notification builder object should need any icon to show it in the status bar. It has a certain mandatory function like setSmallIcon(int). It takes integer argument from R.java file. But I need to give direct image URL without using R.java file. Code:

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

Without R.drawable.ic_launcher. How can I achieve this?

  • As per new behavior of notification small icon is compulsory . That to in white color or in dark gray color with alpha if you want to show image from web in your notification you can use BIGPicture style your notification where you can show image in bellow your your notification. like flipkart show in his notification – Vishal Mokal Feb 17 '16 at 07:17
  • Looking at the source for setSmallIcon, I only see setSmallIcon(int icon) and setSmallIcon(int icon, int level) - so you have to specify a resource. You might be able to do this dynamically at runtime, but I think that will be hacky. – Ewald Feb 17 '16 at 07:21
  • Thanks for the reply. If I remove function setSmallIcon () my app get crashed. But R.drawable.ic_launcher is part of R.java file. I need not access R.java drawable Integer value. I need to set image path for setSmallIcon (or) without setSmallIcon function how can i set notification image – Sudarsan Denver Feb 17 '16 at 09:21
  • small icon is compulsory and it has to be in your application drawable folder that is what the new behavior is you can change large icon and set as per your requirement . small icon will be displayed at right bottom of your large icon in case lollipop and up device and right bottom corner of your entire notification in pre-lollipop devices I have tried this – Vishal Mokal Feb 17 '16 at 11:17

2 Answers2

1

Refer Following Code

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

Small icon is compulsory i tried removing that icon but my app crashed ** **Refer line

 mBuilder.setLargeIcon(BitmapFactory.decodeResource(getBitmapFromURL("YOUR URL"));
Vishal Mokal
  • 792
  • 1
  • 5
  • 22
1

First of all, you should download your image: https://github.com/koush/ion

Then:

Icon ic = Icon.createWithContentUri("uri of your downloaded image");  
builder.setSmallIcon(ic);
Alessandro Verona
  • 1,157
  • 9
  • 23