Skip to content Skip to sidebar Skip to footer

Android Notification Without R.java

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 setSmallIc

Solution 1:

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

Then:

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

Solution 2:

Refer Following Code

privatevoidsendNotification(Bundle extras) {
    IntentmyIntent=null;
    Bundlebundle= extras;
    Set<String> set = bundle.keySet();
    for (String s : set) {
        Log.d("bundle", s);
    }
    Stringtitle= bundle.getString("title");
    Stringmessage= bundle.getString("detail_text");
    StringimageUrl= bundle.getString("image");
    Stringurl= 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 = newIntent(this, SplashScreenActivity.class);
        notificationIntent.putExtra("url", url);
        notificationIntent.putExtra("title", title);
        notificationIntent.setAction("FROM_NOTIFICATION");
    } else {
        notificationIntent = newIntent(this, SplashScreenActivity.class);
        notificationIntent.setAction("FROM_NONE_NOTIFICATION");
    }
    LogUtils.logD(title + " :  " + url);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntentcontentIntent= PendingIntent.getActivity(this, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.BuildermBuilder=newNotificationCompat.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));
    }*/finalintversion= 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");
    Uriuri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    mBuilder.setSound(uri);
    mBuilder.setPriority(Notification.PRIORITY_MAX);
    // mBuilder.setV
    mBuilder.setContentTitle(title);
    mBuilder.setContentText(message);
    NotificationCompat.BigPictureStyles=newNotificationCompat.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 {
        URLurl=newURL(strURL);
        HttpURLConnectionconnection= (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStreaminput= connection.getInputStream();
        BitmapmyBitmap= BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        returnnull;
    }
}

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

mBuilder.setLargeIcon(BitmapFactory.decodeResource(getBitmapFromURL("YOUR URL"));

Post a Comment for "Android Notification Without R.java"