Android Notification Without R.java

2019-07-13 05:38发布

问题:

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?

回答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);


回答2:

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"));