【问题标题】:Android Notification Without R.java没有 R.java 的 Android 通知
【发布时间】:2016-05-28 18:23:26
【问题描述】:

我需要在 Android 中显示本地通知。但是通知构建器对象应该需要任何图标才能在状态栏中显示它。它具有一定的强制功能,如setSmallIcon(int)。它从R.java 文件中获取整数参数。但我需要在不使用 R.java 文件的情况下提供直接图像 URL。 代码:

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

没有 R.drawable.ic_launcher。我怎样才能做到这一点?

【问题讨论】:

  • 根据通知小图标的新行为是强制性的。如果您想在通知中显示来自网络的图像,则可以使用白色或带 alpha 的深灰色,您可以使用 BIGPicture 样式的通知,您可以在通知下方显示图像。就像他通知中的 Flipkart 节目一样
  • 查看 setSmallIcon 的来源,我只看到 setSmallIcon(int icon) 和 setSmallIcon(int icon, int level) - 所以你必须指定一个资源。您也许可以在运行时动态地执行此操作,但我认为这将是 hacky。
  • 感谢您的回复。如果我删除函数 setSmallIcon () 我的应用程序会崩溃。但 R.drawable.ic_launcher 是 R.java 文件的一部分。我不需要访问 R.java 可绘制整数值。我需要为没有 setSmallIcon 功能的 setSmallIcon(或)设置图像路径如何设置通知图像
  • 小图标是强制性的,它必须在您的应用程序可绘制文件夹中,这是您可以更改大图标并根据您的要求设置的新行为。小图标将显示在您的大图标的右下角,以防棒棒糖和向上设备以及您在棒棒糖前设备中的整个通知的右下角我已经尝试过这个

标签: java android notifications r.java-file


【解决方案1】:

首先,您应该下载您的图片: https://github.com/koush/ion

然后:

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

【讨论】:

  • 我无法为 setSmallIcon 设置图标值。因为它的参数是整数
【解决方案2】:

参考以下代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 2013-05-23
    • 2011-02-20
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 2011-11-01
    相关资源
    最近更新 更多