2

I want to create an app that will show internet speed as notification in status bar the problem is that when i create notification it only shows icon but i think this is not the way (if you think there is a way using this then how ?) i used this codes snippet to show notification but what changes to do if i want the result that others are getting as you can see also in screen shot in the leftmost side :

Intent intent = new Intent(this, MainActivity.class);
    PendingIntent contenIntent = PendingIntent.getActivity(this,
            0, intent, 0);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_ID_2)
            .setLargeIcon(getBitmapFromString("22", 20, this))
            .setContentTitle("Title")
            .setContentText("Description")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(contenIntent)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setTicker("23")
            .setSmallIcon(android.R.drawable.screen_background_light_transparent)
            .setOnlyAlertOnce(true);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(1, notification.build());

status bar screen shot

The text changes in notification per second and it is not fake. I have already tried this solution but not accurate since it overlaps with notification icon. Any help will be appreciated thanks in advance.

Md Aman
  • 340
  • 3
  • 10

1 Answers1

0

You can draw the text that you want to display on a bitmap and then set that as an icon to the notification.

NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_ID_2);

notification.setSmallIcon(Icon.createWithBitmap(getBitmapIcon()))

implement getBitmapIcon() and return the bitmap icon that you want to display.

Ismail Shaikh
  • 482
  • 4
  • 13
  • I have already tried this `notification.setSmallIcon(Icon.createWithBitmap(getBitmapIcon()))` but setSmallIcon only says wrong argument type i.e, it requires int how to solve this problem. – Md Aman May 28 '20 at 08:39
  • 1
    And also setSmallIcon(Bitmap bitmap) is added in api level 23 but the other app like from screen shot above i am able to run the app in api level 22 also.How they do that ? – Md Aman May 28 '20 at 08:49
  • I am not sure about this but they might have used this class : https://developer.android.com/reference/android/service/notification/StatusBarNotification It is deprecated now though. – cgb_pandey May 28 '20 at 09:11
  • @MdAman, for api level >= 23, use the `setSmallIcon(Bitmap bitmap)` and get the `bitmap` object like explained here : https://stackoverflow.com/a/45376679/9167710 – cgb_pandey May 28 '20 at 09:14
  • @cgb_pandey .setSmallIcon(Icon.createWithBitmap(bitmap)) is throwing error message says, (int) can not be applied to (android.grapjics.Bitmap) – Md Aman May 28 '20 at 11:04
  • You said earlier that there exists a method `setSmallIcon(Bitmap bitmap)` which was added in api level 23. Just now, I tried and I didn't found it. Without such method, I think it is difficult to show the text as icon in status bar. – cgb_pandey May 28 '20 at 11:10