Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: iOS problem get Firebase Device Token



Permlink Replies: 2 - Last Post: Jul 17, 2017 6:33 AM Last Post By: Lena Ilicheva Threads: [ Previous | Next ]
Lena Ilicheva

Posts: 77
Registered: 9/22/06
iOS problem get Firebase Device Token
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 21, 2017 10:15 PM
Hi.
In Android project all fine but in IOS FDeviceToken always empty.
Where could be the problem?
//h file
    #include <System.JSON.hpp>
    #include <System.PushNotification.hpp>
    //***
    private:   // User declarations
       String FDeviceID;
       String FDeviceToken;
 
       TPushService * FPushService;
       TPushServiceConnection * FPushServiceConnection;
 
       void __fastcall OnReceiveNotificationEvent(TObject *Sender, TPushServiceNotification* const ANotification);
       void __fastcall OnServiceConnectionChange(TObject *Sender, TPushService::TChanges AChange);
       void __fastcall PushServiceRegister();
       void __fastcall RegisterDevice();
 


//cpp
#include <memory>
#include <System.Threading.hpp>
 
#if defined(__ANDROID__)
#include <FMX.PushNotification.Android.hpp>
#include <FMX.Platform.Android.hpp>
 
// Workaround for RSP-17714
namespace Fmx {
 namespace Pushnotification {
   namespace Android {
   _INIT_UNIT(Fmx_Pushnotification_Android);
   }
 }
} // End-of-Workaround
 
#endif
 
#if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
#include <FMX.PushNotification.IOS.hpp>
#include <FMX.Platform.IOS.hpp>
 
// Workaround for RSP-17714
namespace Fmx {
 namespace Pushnotification {
  namespace Ios {
   _INIT_UNIT(Fmx_Pushnotification_Ios);
   }
  }
} // End-of-Workaround
 
#endif
 
const String FAndroidServerKey = L"820629486434"; //for Android
// ---------------------------------------------------------------------------
void ClearAllNotification()
{
  std::unique_ptr<TNotificationCenter> aNotificationCenter(new TNotificationCenter(NULL));
  if(aNotificationCenter->Supported())
   {
   aNotificationCenter->ApplicationIconBadgeNumber = -1;
   aNotificationCenter->CancelAll();
   }
}
 
// ---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
  ClearAllNotification();
  PushServiceRegister();
}
// ---------------------------------------------------------------------------
 
#if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
   String sPlatform = L"IOS";
#elif defined(__ANDROID__)
   String sPlatform = L"ANDROID";
#else
   String sPlatform = L"";
#endif
 
void __fastcall  TForm1::RegisterDevice()
{
   TTask::Run([this]()
    {
     String link = Format(_D("http://blog.rzaripov.kz/pushTest/api.php?method=saveToken&deviceID=%s&deviceToken=%s&platform=%s"),
                        ARRAYOFCONST(( Form1->FDeviceID, Form1->FDeviceToken, sPlatform )));
 
 
     std::unique_ptr<THTTPClient> aHTTP(THTTPClient::Create());
     aHTTP->Get(link);
    }
   );
}
 
// ---------------------------------------------------------------------------
 
void __fastcall  TForm1::PushServiceRegister()
{
 
    FPushService = nullptr;
    FPushServiceConnection = nullptr;
 
   #if defined(__ANDROID__)
    FPushService = TPushServiceManager::Instance->GetServiceByName(TPushService_TServiceNames_GCM);
    if(FPushService)
      FPushService->AppProps[TPushService_TAppPropNames_GCMAppID] = FAndroidServerKey;
   #endif
   #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
      FPushService = TPushServiceManager::Instance->GetServiceByName(TPushService_TServiceNames_APS);
   #endif
 
   if(FPushService)
     {
 
      FPushServiceConnection = new TPushServiceConnection(FPushService);
      FPushServiceConnection->OnChange = &OnServiceConnectionChange;
      FPushServiceConnection->OnReceiveNotification = &OnReceiveNotificationEvent;
      FPushServiceConnection->Active = true;
 
      FDeviceID = FPushService->DeviceIDValue[TPushService_TDeviceIDNames_DeviceID];
      FDeviceToken = FPushService->DeviceTokenValue[TPushService_TDeviceTokenNames_DeviceToken];//empty!!!
 
      if(FDeviceID != "" && FDeviceToken != "")
       {
        RegisterDevice();
       }
 
     }
 
   
}
 
// ---------------------------------------------------------------------------
void __fastcall TForm1::OnServiceConnectionChange(TObject *Sender, TPushService::TChanges AChange)
{
   if (AChange.Contains(TPushService::TChange::DeviceToken) && (FPushServiceConnection))
   {
    FDeviceID = FPushService->DeviceIDValue[TPushService_TDeviceIDNames_DeviceID];
    FDeviceToken =   FPushService->DeviceTokenValue[TPushService_TDeviceTokenNames_DeviceToken];//empty!!!
 
      if(FDeviceID != "" && FDeviceToken != "")
       {
        RegisterDevice();
       }
 
   }
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::OnReceiveNotificationEvent(TObject *Sender,
   TPushServiceNotification* const ANotification)
{
 
 ClearAllNotification();
 
 const String FCMSignature = L"gcm.notification.body";
 const String GCMSignature = L"message";
 const String APNsSignature = L"alert";
 String aText = "";
 TJSONValue * aObj;
 
   #if defined(__ANDROID__)
      aObj = ANotification->DataObject->GetValue(GCMSignature);
      if(aObj != NULL)
         {
         aText = aObj->Value();
         }
         else
           {
            aText = ANotification->DataObject->GetValue(FCMSignature)->Value();
           }
   #endif
 
   #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
     aObj = ANotification->DataObject->GetValue(APNsSignature);
     if (aObj != NULL)
      {
      aText = aObj->Value();
      }
    #endif
 
 ShowMessage(aText);
 
}


–°++ Builder Berlin update 2.
FDeviceToken in ios project is empty. :(

P.S. For set Android and IOS I used:
http://blog.rzaripov.kz/2017/02/firebase-android-ios.html
http://blog.rzaripov.kz/2017/02/firebase-android-ios-2.html
Problem only in IOS project.

Lena Ilicheva

Posts: 77
Registered: 9/22/06
Re: iOS problem get Firebase Device Token
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 25, 2017 7:57 AM   in response to: Lena Ilicheva in response to: Lena Ilicheva
Lena Ilicheva

Posts: 77
Registered: 9/22/06
Re: iOS problem get Firebase Device Token
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 13, 2017 1:01 AM   in response to: Lena Ilicheva in response to: Lena Ilicheva
I set correct provisioning profile and got FDeviceToken.
Now FDeviceToken is not NULL.
But when I try send push from Firebase to my IOS10 devise notthing happens.
Can sombody show how get push from FCM to IOS10 devise?
–°++ Builder Berlin UP2.
https://stackoverflow.com/questions/39504334/push-notification-not-receiving-in-ios-10
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02