我在Android设备上的推送通知中的小图标有问题。 图标显示为黑色。
我已经按照How to change small icon image in notification on Android device with DELPHI这篇文章中pudnivec74指示的步骤进行操作,并且可以使用,但是仅当应用程序正在运行时,当应用程序关闭时,图标仍然显示为黑色。
我还在文件AndroidManifest.template.xml中添加了以下几行:
<meta-data android: name = "com.google.firebase.messaging.default_notification_icon" android: resource = "@ drawable / ic_notification" />
并且保持不变。
有人知道解决方案吗?
这是我使用的代码:
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;