首页 > 解决方案 > 使用 Delphi XE 10.3 Rio 在 Android 上更改推送通知中的小图标

问题描述

我对 Android 设备上的推送通知中的小图标有疑问。图标显示为黑色。

我已经按照 pudnivec74 在这篇文章How to change small icon image in notice on Android device with DELPHI 中指示的步骤进行操作,并且它可以工作,但只有在应用程序运行时,当应用程序关闭时,图标仍然显示为黑色。

我还在文件 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;    

标签: androiddelphipush-notificationfiremonkey

解决方案


推荐阅读