-1

I'm trying to create a dynamic Icon for my notifications using a function that creates a Bitmap from a String.

When I started my project, I wanted to use API level 22, but I realized that public Notification.Builder setSmallIcon (Icon icon) was not available until API 23. So I changed my MinSDKVersion, but i'm still getting that incompatible types error ...

package com.example.netmetah;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;
import android.app.Notification.Builder;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class NetMonitorService extends IntentService {

    public NetMonitorService() {
        super("NetMeterListening");
    }

    @Override
    protected void onHandleIntent(Intent workIntent) {
        Context context = getApplicationContext();
        CharSequence text = "Le service marche";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        createNotification(createBitmapFromString("23","kb"));
    }

    private Bitmap createBitmapFromString(String speed, String units) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(55);
        paint.setTextAlign(Paint.Align.CENTER);

        Paint unitsPaint = new Paint();
        unitsPaint.setAntiAlias(true);
        unitsPaint.setTextSize(40);
        unitsPaint.setTextAlign(Paint.Align.CENTER);

        Rect textBounds = new Rect();
        paint.getTextBounds(speed, 0, speed.length(), textBounds);

        Rect unitsTextBounds = new Rect();
        unitsPaint.getTextBounds(units, 0, units.length(), unitsTextBounds);

        int width = (textBounds.width() > unitsTextBounds.width()) ? textBounds.width() : unitsTextBounds.width();

        Bitmap bitmap = Bitmap.createBitmap(width + 10, 90,
                Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);
        canvas.drawText(speed, width / 2 + 5, 50, paint);
        canvas.drawText(units, width / 2, 90, unitsPaint);

        return bitmap;
    }

    public static long[] readProc() {
        BufferedReader br;
        String line;
        String[] lines;
        long rx = 0;
        long tx = 0;
        try {
            br = new BufferedReader(new FileReader("/proc/net/dev"));
            while ((line = br.readLine()) != null) {
                if (line.contains("eth") || line.contains("wlan")) {
                    lines = line.trim().split("\\s+");
                    rx += Long.parseLong(lines[1]);
                    tx += Long.parseLong(lines[9]);
                }
            }
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        long[] bytes = {rx, tx};
        return bytes;
    }

    private void createNotification(Bitmap icon) {
        Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        notificationIntent.setData(Uri.parse("http://www.google.ca"));
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, "fuck")
                .setCategory(Notification.CATEGORY_PROMO)
                .setSmallIcon(Icon.createWithBitmap(icon))
                .setContentTitle("C'est quoi un titre")
                .setContentText("C'est quoi un text")
                .setAutoCancel(true)
                .setVisibility(1) //Public
                .addAction(android.R.drawable.ic_menu_view, "View details", contentIntent)
                .setContentIntent(contentIntent)
                .setPriority(Notification.PRIORITY_HIGH)
                .setChannelId("fuck")
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}).build();
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(123, notification);

    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • possible duplicate of - https://stackoverflow.com/questions/16055073/set-drawable-or-bitmap-as-icon-in-notification-in-android/16055373#16055373 – Rajshree Tiwari Apr 18 '19 at 16:01
  • No it doesn't answer my question, In fact, I was originally using this post as a guideline to make this work – Dannick Larochelle Apr 18 '19 at 16:07
  • So in your code, are you showing icon in bitmap form, is that icon coming from server? – Rajshree Tiwari Apr 18 '19 at 16:09
  • I'm generating it from a String using ** private Bitmap createBitmapFromString(String speed, String units)** The created Bitmap works with setLargeIcon() but not with setSmallIcon() It's like I was still on API 22, I can't use the "new" overloaded function from NotificationCompat.Builder – Dannick Larochelle Apr 18 '19 at 16:16
  • In the developer blog, in Notification section , it has mention if you want to use setSmallIcon() for that you need to pass R.drawable.image , because it exist in your package, so what is the problem having with large icon? – Rajshree Tiwari Apr 18 '19 at 16:22
  • As I said, I don't have any problem with setLargeIcon() The doc says that I can provide a Icon object for setSmallIcon() instead [link]https://developer.android.com/reference/android/app/Notification.Builder.html#setSmallIcon(android.graphics.drawable.Icon)[/link] **public Notification.Builder setSmallIcon (Icon icon)** – Dannick Larochelle Apr 18 '19 at 16:26
  • I am also looking for your solution, if i get i will tell :) – Rajshree Tiwari Apr 18 '19 at 16:48

1 Answers1

0

You are using NotificationCompat.Builder. According to the documentation the setSmallIcon methods take a drawable resource ID.

It's easy to confuse this with the methods for Notification.Builder where one takes a drawable resource ID and the other one an Icon.

TofferJ
  • 4,678
  • 1
  • 37
  • 49