How to change small icon image in notification on Android device with DELPHI
Asked Answered
P

2

4

Is there any way to change the small icon image (default used application icon) in the notification in the Android application with Delphi?

The standard way to easily create a notification is:

var
  aNotification: TNotification;
begin   
  aNotification:= aNotificationCenter.CreateNotification;
  try
    aNotification.Name:= 'Notification Name';
    aNotification.Title:= 'Notification Title';
    aNotification.AlertBody:= 'Notification Alert Body';
    aNotification.EnableSound:= False;
    aWorkNotification.Number:= 1;
    aNotificationCenter.PresentNotification(aNotification);
  finally
    aNotification.Free;
  end;
end;

I know solution for Java, but I don't know how the same programmed in Delphi.

Advise me someone?

Paperweight answered 30/7, 2016 at 20:46 Comment(3)
It is very easy. Simply use NotificationCompat_Builder and then you can use any image (icon) you will want to.Deitz
The next procedure is the same as for JavaDeitz
You say that it is very easy and I can use any image or icon. Can you show me how do it?Paperweight
D
4

First, prepare the icon in png format (16 x 16, named for example ic_test). Save the png icon using the Deployment to these directories:

res\drawable-ldpi\
res\drawable-mdpi\
res\drawable-hdpi\
res\drawable-xhdpi\
res\drawable-xxhdpi\

Then use the following code:

.
.
var
  Ntf: JNotification;
  ntfBuilder: JNotificationCompat_Builder;
  ntfManager: JNotificationManager;
.
.
  ntfBuilder:= TJNotificationCompat_Builder.JavaClass.init(TAndroidHelper.Context);
  ntfBuilder.setSmallIcon(TAndroidHelper.Context.getResources.getIdentifier(StringToJString('ic_test'), StringToJString('drawable'), TAndroidHelper.Context.getPackageName));
  ntfBuilder.setContentTitle(StrToJCharSequence('Content Title'));
  ntfBuilder.setContentText(StrToJCharSequence('Content Test'));
  ntfBuilder.setAutoCancel(True);

  Ntf:= ntfBuilder.build;

  ntfManager:= TJNotificationManager.Wrap((TAndroidHelper.Context.getSystemService(TJContext.JavaClass.NOTIFICATION_SERVICE) as ILocalObject).GetObjectID);
  ntfManager.notify(1, Ntf);
.
.

If you want to use built-in system icons use this constants

const
  r_drawable_alert_dark_frame = 17301504;
  r_drawable_alert_light_frame = 17301505;
  r_drawable_arrow_down_float = 17301506;
  r_drawable_arrow_up_float = 17301507;
  r_drawable_bottom_bar = 17301658;
  r_drawable_btn_default = 17301508;
  r_drawable_btn_default_small = 17301509;
  r_drawable_btn_dialog = 17301527;
  r_drawable_btn_dropdown = 17301510;
  r_drawable_btn_minus = 17301511;
  r_drawable_btn_plus = 17301512;
  r_drawable_btn_radio = 17301513;
  r_drawable_btn_star = 17301514;
  r_drawable_btn_star_big_off = 17301515;
  r_drawable_btn_star_big_on = 17301516;
  r_drawable_button_onoff_indicator_off = 17301518;
  r_drawable_button_onoff_indicator_on = 17301517;
  r_drawable_checkbox_off_background = 17301519;
  r_drawable_checkbox_on_background = 17301520;
  r_drawable_dark_header = 17301669;
  r_drawable_dialog_frame = 17301521;
  r_drawable_dialog_holo_dark_frame = 17301682;
  r_drawable_dialog_holo_light_frame = 17301683;
  r_drawable_divider_horizontal_bright = 17301522;
  r_drawable_divider_horizontal_dark = 17301524;
  r_drawable_divider_horizontal_dim_dark = 17301525;
  r_drawable_divider_horizontal_textfield = 17301523;
  r_drawable_edit_text = 17301526;
  r_drawable_editbox_background = 17301528;
  r_drawable_editbox_background_normal = 17301529;
  r_drawable_editbox_dropdown_dark_frame = 17301530;
  r_drawable_editbox_dropdown_light_frame = 17301531;
  r_drawable_gallery_thumb = 17301532;
  r_drawable_ic_btn_speak_now = 17301668;
  r_drawable_ic_delete = 17301533;
  r_drawable_ic_dialog_alert = 17301543;
  r_drawable_ic_dialog_dialer = 17301544;
  r_drawable_ic_dialog_email = 17301545;
  r_drawable_ic_dialog_info = 17301659;
  r_drawable_ic_dialog_map = 17301546;
  r_drawable_ic_input_add = 17301547;
  r_drawable_ic_input_delete = 17301548;
  r_drawable_ic_input_get = 17301549;
  r_drawable_ic_lock_idle_alarm = 17301550;
  r_drawable_ic_lock_idle_charging = 17301534;
  r_drawable_ic_lock_idle_lock = 17301535;
  r_drawable_ic_lock_idle_low_battery = 17301536;
  r_drawable_ic_lock_lock = 17301551;
  r_drawable_ic_lock_power_off = 17301552;
  r_drawable_ic_lock_silent_mode = 17301553;
  r_drawable_ic_lock_silent_mode_off = 17301554;
  r_drawable_ic_media_ff = 17301537;
  r_drawable_ic_media_next = 17301538;
  r_drawable_ic_media_pause = 17301539;
  r_drawable_ic_media_play = 17301540;
  r_drawable_ic_media_previous = 17301541;
  r_drawable_ic_media_rew = 17301542;
  r_drawable_ic_menu_add = 17301555;
  r_drawable_ic_menu_agenda = 17301556;
  r_drawable_ic_menu_always_landscape_portrait = 17301557;
  r_drawable_ic_menu_call = 17301558;
  r_drawable_ic_menu_camera = 17301559;
  r_drawable_ic_menu_close_clear_cancel = 17301560;
  r_drawable_ic_menu_compass = 17301561;
  r_drawable_ic_menu_crop = 17301562;
  r_drawable_ic_menu_day = 17301563;
  r_drawable_ic_menu_delete = 17301564;
  r_drawable_ic_menu_directions = 17301565;
  r_drawable_ic_menu_edit = 17301566;
  r_drawable_ic_menu_gallery = 17301567;
  r_drawable_ic_menu_help = 17301568;
  r_drawable_ic_menu_info_details = 17301569;
  r_drawable_ic_menu_manage = 17301570;
  r_drawable_ic_menu_mapmode = 17301571;
  r_drawable_ic_menu_month = 17301572;
  r_drawable_ic_menu_more = 17301573;
  r_drawable_ic_menu_my_calendar = 17301574;
  r_drawable_ic_menu_mylocation = 17301575;
  r_drawable_ic_menu_myplaces = 17301576;
  r_drawable_ic_menu_preferences = 17301577;
  r_drawable_ic_menu_recent_history = 17301578;
  r_drawable_ic_menu_report_image = 17301579;
  r_drawable_ic_menu_revert = 17301580;
  r_drawable_ic_menu_rotate = 17301581;
  r_drawable_ic_menu_save = 17301582;
  r_drawable_ic_menu_search = 17301583;
  r_drawable_ic_menu_send = 17301584;
  r_drawable_ic_menu_set_as = 17301585;
  r_drawable_ic_menu_share = 17301586;
  r_drawable_ic_menu_slideshow = 17301587;
  r_drawable_ic_menu_sort_alphabetically = 17301660;
  r_drawable_ic_menu_sort_by_size = 17301661;
  r_drawable_ic_menu_today = 17301588;
  r_drawable_ic_menu_upload = 17301589;
  r_drawable_ic_menu_upload_you_tube = 17301590;
  r_drawable_ic_menu_view = 17301591;
  r_drawable_ic_menu_week = 17301592;
  r_drawable_ic_menu_zoom = 17301593;
  r_drawable_ic_notification_clear_all = 17301594;
  r_drawable_ic_notification_overlay = 17301595;
  r_drawable_ic_partial_secure = 17301596;
  r_drawable_ic_popup_disk_full = 17301597;
  r_drawable_ic_popup_reminder = 17301598;
  r_drawable_ic_popup_sync = 17301599;
  r_drawable_ic_search_category_default = 17301600;
  r_drawable_ic_secure = 17301601;
  r_drawable_list_selector_background = 17301602;
  r_drawable_menu_frame = 17301603;
  r_drawable_menu_full_frame = 17301604;
  r_drawable_menuitem_background = 17301605;
  r_drawable_picture_frame = 17301606;
  r_drawable_presence_audio_away = 17301679;
  r_drawable_presence_audio_busy = 17301680;
  r_drawable_presence_audio_online = 17301681;
  r_drawable_presence_away = 17301607;
  r_drawable_presence_busy = 17301608;
  r_drawable_presence_invisible = 17301609;
  r_drawable_presence_offline = 17301610;
  r_drawable_presence_online = 17301611;
  r_drawable_presence_video_away = 17301676;
  r_drawable_presence_video_busy = 17301677;
  r_drawable_presence_video_online = 17301678;
  r_drawable_progress_horizontal = 17301612;
  r_drawable_progress_indeterminate_horizontal = 17301613;
  r_drawable_radiobutton_off_background = 17301614;
  r_drawable_radiobutton_on_background = 17301615;
  r_drawable_screen_background_dark = 17301656;
  r_drawable_screen_background_dark_transparent = 17301673;
  r_drawable_screen_background_light = 17301657;
  r_drawable_screen_background_light_transparent = 17301674;
  r_drawable_spinner_background = 17301616;
  r_drawable_spinner_dropdown_background = 17301617;
  r_drawable_star_big_off = 17301619;
  r_drawable_star_big_on = 17301618;
  r_drawable_star_off = 17301621;
  r_drawable_star_on = 17301620;
  r_drawable_stat_notify_call_mute = 17301622;
  r_drawable_stat_notify_chat = 17301623;
  r_drawable_stat_notify_error = 17301624;
  r_drawable_stat_notify_missed_call = 17301631;
  r_drawable_stat_notify_more = 17301625;
  r_drawable_stat_notify_sdcard = 17301626;
  r_drawable_stat_notify_sdcard_prepare = 17301675;
  r_drawable_stat_notify_sdcard_usb = 17301627;
  r_drawable_stat_notify_sync = 17301628;
  r_drawable_stat_notify_sync_noanim = 17301629;
  r_drawable_stat_notify_voicemail = 17301630;
  r_drawable_stat_sys_data_bluetooth = 17301632;
  r_drawable_stat_sys_download = 17301633;
  r_drawable_stat_sys_download_done = 17301634;
  r_drawable_stat_sys_headset = 17301635;
  r_drawable_stat_sys_phone_call = 17301636;
  r_drawable_stat_sys_phone_call_forward = 17301637;
  r_drawable_stat_sys_phone_call_on_hold = 17301638;
  r_drawable_stat_sys_speakerphone = 17301639;
  r_drawable_stat_sys_upload = 17301640;
  r_drawable_stat_sys_upload_done = 17301641;
  r_drawable_stat_sys_vp_phone_call = 17301671;
  r_drawable_stat_sys_vp_phone_call_on_hold = 17301672;
  r_drawable_stat_sys_warning = 17301642;
  r_drawable_status_bar_item_app_background = 17301643;
  r_drawable_status_bar_item_background = 17301644;
  r_drawable_sym_action_call = 17301645;
  r_drawable_sym_action_chat = 17301646;
  r_drawable_sym_action_email = 17301647;
  r_drawable_sym_call_incoming = 17301648;
  r_drawable_sym_call_missed = 17301649;
  r_drawable_sym_call_outgoing = 17301650;
  r_drawable_sym_contact_card = 17301652;
  r_drawable_sym_def_app_icon = 17301651;
  r_drawable_title_bar = 17301653;
  r_drawable_title_bar_tall = 17301670;
  r_drawable_toast_frame = 17301654;
  r_drawable_zoom_plate = 17301655;

with the following code (for example, for the android equivalent of R.drawable.ic _menu_send):

  ntfBuilder.setSmallIcon(r_drawable_ic_menu_send);
Deitz answered 6/8, 2016 at 7:10 Comment(2)
All icons in the Deployment must have the same remote name - for example ic_test like in this answer ;-)Deitz
uses Androidapi.JNI.App, {JNotification & co.} Androidapi.JNI.Support, {JNotificationCompat_Builder} Androidapi.Helpers, {TAndroidHelper & co.} Androidapi.JNI.GraphicsContentViewText, {TJContext} Androidapi.JNIBridge; {ILocalObject}Igneous
F
0

Because it didn't work for me on Android 11 with Delphi 10.3, I made changes at System.Notification and System.Android.Notification files;

(I copied them to my path and added them to my app so the original files are fine)

System.Notification changes:

I added a new public field to the TNotification:

SmallIconID : Integer;

and in the constructor I initialized with -1;

SmallIconId := -1;

System.Android.Notification changes:

in the function TNotificationCenterAndroid.CreateNativeNotification I added this code before the if TOSVersion.Check(5) line:

if ANotification.SmallIconID > -1 then
  Builder := Builder.setSmallIcon(ANotification.SmallIconID);

and now I can set up the small icon from my app and it's working on Android 11 as well.

Forde answered 7/1, 2022 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.