Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: event OnReceiveNotificationEvent occurs two times in a row



Permlink Replies: 0
Lena Ilicheva

Posts: 77
Registered: 9/22/06
event OnReceiveNotificationEvent occurs two times in a row
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 19, 2017 7:21 AM
Hi.
I found information how to send native push without Kinvey (in Russian):
http://blog.rzaripov.kz/2017/02/firebase-android-ios.html
http://blog.rzaripov.kz/2017/02/firebase-android-ios-2.html
PHP server:
https://github.com/rzaripov1990/PUSHTestFCM

Everyone who writes a Delphi event OnReceiveNotificationEvent happens once.
When I send push:
 
http://mysite.com/pushTest/api.php?method=sendPush&title=My title&text=Custom text


in my C++ code event OnReceiveNotificationEvent occurs two times in a row!
Is this the next mistake idera in C++ Builder or my?
 
//h file
#include <System.Net.HttpClient.hpp>
#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 file
#include <memory>
#include <System.Threading.hpp>
 
#if defined(__ANDROID__)
#include <FMX.Platform.Android.hpp>
#include <FMX.PushNotification.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.Platform.IOS.hpp>
#include <FMX.PushNotification.IOS.hpp>
 
namespace Fmx {
 namespace Pushnotification {
  namespace Ios {
   _INIT_UNIT(Fmx_Pushnotification_Ios);
   }
  }
}
 
TForm1 *Form1;
 
const String FAndroidServerKey = L"820629486436";//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();
}
// ---------------------------------------------------------------------------
 
bool __fastcall CheckInet()
{
   bool result = false;
   THTTPClient *aHTTP = THTTPClient::Create();
   try {
      try {
         _di_IHTTPResponse aResp = aHTTP->Head("http://google.com");
         result = (aResp->StatusCode < 400);
      }
      catch (const System::Sysutils::Exception &E) {
         result = false;
      }
   }
   __finally {
      delete aHTTP;
   }
   return result;
}
 
// ---------------------------------------------------------------------------
 
//*** for TTask::Run ***
#if defined(_PLAT_IOS)
   String sPlatform = L"IOS";
#elif defined(__ANDROID__)
   String sPlatform = L"ANDROID";
#else
   String sPlatform = L"";
#endif
 
class TCppTask : public TCppInterfacedObject<TProc>
{
 String FDeviceID, FDeviceToken;
 public:
    TCppTask(String AnID, String AToken) : FDeviceID(AnID), FDeviceToken(AToken)
    {
    }
    void __fastcall Invoke()
    {
      String link =
      String().sprintf(L"http://mysite.com/pushTest/api.php?method=saveToken&deviceID=%s&deviceToken=%s&platform=%s",
                   UTF8String(FDeviceID).c_str(), UTF8String(FDeviceToken).c_str(), UTF8String(sPlatform).c_str());
 
      std::unique_ptr<THTTPClient> aHTTP(THTTPClient::Create());
      aHTTP->Get(link);
    }
};
 
// ---------------------------------------------------------------------------
void __fastcall  TForm1::RegisterDevice()
{
  TTask::Run(_di_TProc(new TCppTask(FDeviceID, FDeviceToken)));
}
//*** end TTask::Run ***
// ---------------------------------------------------------------------------
 
void __fastcall  TForm1::PushServiceRegister()
{
   bool result = CheckInet();
 
   if (result == true)
   {
    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];
 
      if(FDeviceID != "" && FDeviceToken != "")
       {
        RegisterDevice();
       }
 
     }
 
   }//if (result == true)
}
 
// ---------------------------------------------------------------------------
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];
 
      if(FDeviceID != "" && FDeviceToken != "")
       {
        RegisterDevice();
       }
 
   }
}
// ---------------------------------------------------------------------------
 
void __fastcall TForm1::OnReceiveNotificationEvent(TObject *Sender,
   TPushServiceNotification* const ANotification)
{
 
 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);//twice in a row!!!
 
}
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02