2015-12-01 72 views
1

我想包括Apple Push Notifications在我的iOS應用程序後面this example德爾福蘋果推送通知不工作在iOS上,而在Android上工作

經過一些調試後,我發現APushServiceiOS上運行時爲空,但在Android上運行時沒有。

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

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

end; 

我做錯了什麼?我們如何在Delphi上啓用iOS的推送通知?

任何想法將不勝感激。

+0

怎麼可能讓'GetServiceByName'找不到服務? – Machado

+1

我不知道。我沒有源代碼,但是你有。嘗試尋找* System.PushNotification.pas *尋找線索。也許該名稱的服務不存在。也許你的應用程序沒有足夠的權限來查看結果。 –

回答

2

該解決方案對於我們解決的工作量而言非常簡單。

FMX.PushNotification.iOS代碼中缺少導入。

有趣的是:如果我們刪除PushNotifications進口,Delphi Seattle將不會顯示任何警告。

0

這裏工作,我使用的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; 

下面是詳細步驟通過Android和iOS(使用谷歌翻譯)對C步文章++和Delphi代碼就可以使用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/