Change small icon in push notification on Android with Delphi XE 10.3 Rio

  

I have a problem with the small icon in push notification on Android devices.
The icon shows black.

I have followed the steps indicated by pudnivec74 in this post How to change small icon image in notification on Android device with DELPHI , and it works, but only when application is running, when the application is closed the icon stills appears black.

I have also added in the file AndroidManifest.template.xml these lines:

<meta-data android: name = “com.google.firebase.messaging.default_notification_icon” android: resource = “@ drawable / ic_notification” />

and remains the same.

Does anyone know the solution?

Here is the code that I used:

procedure TFrmMain.FormCreate(Sender: TObject);
begin
try
{$IF DEFINED(ANDROID)}
APushService := TPushServiceManager.Instance.GetServiceByName(TPushService.TServiceNames.GCM);
APushService.AppProps[TPushService.TAppPropNames.GCMAppID] :=UResource.NUMEROPROYECTOANDROID;
{$ELSE}
APushService := TPushServiceManager.Instance.GetServiceByName(TPushService.TServiceNames.APS);
{$ENDIF}

AServiceConnection := TPushServiceConnection.Create(APushService);
AServiceConnection.Active := True;

{$IF DEFINED(ANDROID)}
AServiceConnection.OnReceiveNotification := OnReceiveNotificationPushAndroid;
{$ELSE}
AServiceConnection.OnReceiveNotification := OnReceiveNotificationPushiOS;
{$ENDIF}

VKAutoShowMode := TVKAutoShowMode.Always;
except on E: Exception do
end;
end;

procedure TFrmMain.OnReceiveNotificationPushAndroid(Sender: TObject;
const ANotification: TPushServiceNotification);
var
{$IFDEF ANDROID}
Ntf: JNotification;
ntfBuilder: JNotificationCompat_Builder;
ntfManager: JNotificationManager;
{$ENDIF}
Mensaje, Titulo: String;
begin
{$IFDEF ANDROID}
try
//ShowMessage(ANotification.Json.ToString);
Titulo := Copy(ANotification.Json.GetValue(‘title’).ToString, 2, Length(ANotification.Json.GetValue(‘title’).ToString)-2);
Mensaje := Copy(ANotification.Json.GetValue(‘message’).ToString, 2, Length(ANotification.Json.GetValue(‘message’).ToString)-2);

ntfBuilder:= TJNotificationCompat_Builder.JavaClass.init(TAndroidHelper.Context);
ntfBuilder.setSmallIcon(TAndroidHelper.Context.getResources.getIdentifier(StringToJString(‘ic_notification’), StringToJString(‘drawable’), TAndroidHelper.Context.getPackageName));
ntfBuilder.setContentTitle(StrToJCharSequence(Titulo));
ntfBuilder.setContentText(StrToJCharSequence(Mensaje));

ntfBuilder.setAutoCancel(True);

Ntf:= ntfBuilder.build;
Ntf.defaults := TJNotification.JavaClass.DEFAULT_SOUND;
if Ntf.defaults <> TJNotification.JavaClass.DEFAULT_VIBRATE then
Ntf.defaults := Ntf.defaults + TJNotification.JavaClass.DEFAULT_VIBRATE;

ntfManager:= TJNotificationManager.Wrap((TAndroidHelper.Context.getSystemService(TJContext.JavaClass.NOTIFICATION_SERVICE) as ILocalObject).GetObjectID);
ntfManager.notify(1, Ntf);
except on E: Exception do
end;

{$ENDIF}
end;

Comments are closed.