【问题标题】:Delphi Apple Push Notification not working on iOS while working on Android在 Android 上工作时,Delphi Apple 推送通知在 iOS 上不起作用
【发布时间】:2016-03-05 17:31:59
【问题描述】:

我正在尝试将Apple Push Notifications 包含在我的iOS 应用程序中,跟在this example 之后。

经过一些调试后,我发现APushServiceiOS 上运行时为空,但在Android 上却不是。

procedure TFormLogin.Button1Click(Sender: TObject);
var
    APushService           : TPushService;
begin

    APushService       := TPushServiceManager.Instance.GetServiceByName(TPushService.TServiceNames.APS);

end;

我做错了吗?我们如何在 Delphi 上启用 iOS 推送通知?

任何想法都将受到高度赞赏。

【问题讨论】:

  • 什么可能导致GetServiceByName 找不到服务?
  • 我不知道。我没有源代码,但你有。尝试在 System.PushNotification.pas 中寻找线索。也许具有该名称的服务不存在。也许您的应用没有足够的权限来查看结果。

标签: ios delphi apple-push-notifications firemonkey


【解决方案1】:

对于我们花费大量精力来解决的问题,解决方案非常简单。

代码中缺少FMX.PushNotification.iOS 导入。

有趣的是:如果我们删除 PushNotifications 导入,Delphi Seattle 将不会显示任何警告。

【讨论】:

    【解决方案2】:

    这是我用于 Android 和 iOS 的工作代码:

    const
      FAndroidServerKey = '63538920422';
    
    private
      { Private declarations }
      FDeviceID: string;
      FDeviceToken: string;
    
      FPushService: TPushService;
      FPushServiceConnection: TPushServiceConnection;
    
      procedure OnReceiveNotificationEvent(Sender: TObject; 
        const ANotification: TPushServiceNotification);
      procedure OnServiceConnectionChange(Sender: TObject; 
        AChange: TPushService.TChanges);
    
      procedure PushServiceRegister;
    
    procedure TFormMain.OnReceiveNotificationEvent(Sender: TObject; 
      const ANotification: TPushServiceNotification);
    const
      FCMSignature = 'gcm.notification.body';
      GCMSignature = 'message';
      APNsSignature = 'alert';
    var
      aText: string;
      aObj: TJSONValue;
    begin
      // this will fire when only when app is opened
    {$IFDEF ANDROID}
      aObj := ANotification.DataObject.GetValue(GCMSignature);
      if aObj <> nil then
        aText := aObj.Value
      else
        aText := ANotification.DataObject.GetValue(FCMSignature).Value;
    {$ELSE}
      aObj := ANotification.DataObject.GetValue(APNsSignature);
      if aObj <> nil then
        aText := aObj.Value;
    {$ENDIF}
      ShowMessage(aText);
    end;
    
    procedure TFormMain.OnServiceConnectionChange(Sender: TObject; 
      AChange: TPushService.TChanges);
    begin
      if (TPushService.TChange.DeviceToken in AChange) and
        Assigned(FPushServiceConnection) then
      begin
        FDeviceID := FPushService.DeviceIDValue[TPushService.TDeviceIDNames.DeviceID];
        FDeviceToken := FPushService.DeviceTokenValue
          [TPushService.TDeviceTokenNames.DeviceToken];
        // save token and ID to  remote db here
      end;
    end;
    
    procedure TFormMain.PushServiceRegister;
    begin
      FPushService := nil;
      FPushServiceConnection := nil;
    
    {$IF defined(ANDROID)}
      FPushService := TPushServiceManager.Instance.GetServiceByName<
        (TPushService.TServiceNames.GCM);
      FPushService.AppProps[TPushService.TAppPropNames.GCMAppID] := FAndroidServerKey;
    {$ENDIF}
    {$IF defined(IOS) AND defined(CPUARM)}
      FPushService := TPushServiceManager.Instance.GetServiceByName
       (TPushService.TServiceNames.APS);
    {$ENDIF}
      if Assigned(FPushService) then
      begin
        FPushServiceConnection := TPushServiceConnection.Create(FPushService);
        FPushServiceConnection.OnChange := OnServiceConnectionChange;
        FPushServiceConnection.OnReceiveNotification := OnReceiveNotificationEvent;
        FPushServiceConnection.Active := true; // this will fires OnChange event
      end;
    end;
    

    这里是针对 C++ 和 Delphi 代码的 Android 和 iOS 的详细分步文章(使用谷歌翻译)以及准备使用的 PHP 服务器代码。

    http://blog.rzaripov.kz/2017/02/firebase-android-ios.html http://blog.rzaripov.kz/2017/02/firebase-android-ios-2.html

    使用此论坛提问(您可以使用英语) http://fire-monkey.ru/topic/1809-статья-php-сервер-для-рассылки-push-на-android-и-ios/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多