event OnReceiveNotificationEvent occurs two times in a row

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Post Reply
Lena
BCBJ Master
BCBJ Master
Posts: 696
Joined: Sun Feb 06, 2011 1:28 pm

event OnReceiveNotificationEvent occurs two times in a row

Post by Lena »

Hi.
I found information how to send native push without Kinvey (but in Russian from http://fire-monkey.ru):
http://blog.rzaripov.kz/2017/02/firebas ... d-ios.html
http://blog.rzaripov.kz/2017/02/firebas ... ios-2.html
Everyone who writes a Delphi event OnReceiveNotificationEvent happens once.
When I send push:

Code: Select all

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 or my?

Code: Select all

//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"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();
}
// ---------------------------------------------------------------------------

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)
{

 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);//twice in a row!!!


}

Post Reply