The native push

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

The native push

Postby Lena » Sat Mar 18, 2017 7:50 am

Hi.
I found information how to send native push without Kinvey (but in Russian from http://fire-monkey.ru):
Part1
Part2
In the Part2, all the code on Delphi and no one in the Russian forum can translate it into C++. :(
Please help translate the code into C++.
Lena
BCBJ Master
BCBJ Master
 
Posts: 454
Joined: Sun Feb 06, 2011 1:28 pm

Re: The native push

Postby Lena » Mon Mar 20, 2017 8:06 am

How it will be on С++?

Code: Select all
if (TPushService.TChange.DeviceToken in AChange) and
    Assigned(FPushServiceConnection)


//***
 procedure OnReceiveNotificationEvent(Sender: TObject;
    const ANotification: TPushServiceNotification);
  procedure OnServiceConnectionChange(Sender: TObject;
    AChange: TPushService.TChanges);
Lena
BCBJ Master
BCBJ Master
 
Posts: 454
Joined: Sun Feb 06, 2011 1:28 pm

Re: The native push

Postby rlebeau » Mon Mar 20, 2017 1:59 pm

Lena wrote:How it will be on С++?


Try something like this:

Code: Select all
#include <System.PushNotification.hpp>
#if defined(__ANDROID__)
#include <FMX.PushNotification.Android.hpp>
#endif
#if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
#include <FMX.PushNotification.IOS.hpp>
#endif

...

const String FAndroidServerKey = L"63538920422";

...

private:
    // Private 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 TFormMain::OnReceiveNotificationEvent(TObject *Sender, TPushServiceNotification* const ANotification)
{
    ShowMessage(ANotification->Json->ToString());
}
 
void __fastcall TFormMain::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];
    }
}

void __fastcall TFormMain::PushServiceRegister()
{
    FPushService = nullptr;
    FPushServiceConnection = nullptr;
 
    #if defined(__ANDROID__)
    FPushService = TPushServiceManager::Instance->GetServiceByName(TPushService_TServiceNames_GCM);
    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];
    }
}
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1402
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: The native push

Postby Lena » Tue Mar 21, 2017 8:43 am

Thank You very much!
Lena
BCBJ Master
BCBJ Master
 
Posts: 454
Joined: Sun Feb 06, 2011 1:28 pm

Re: The native push

Postby Lena » Mon Mar 27, 2017 7:05 am

I'm looking for information on how to send push in C++ Builder Berlin.
I combined two articles:
Russian 1
Russian 2

I used the code rlebeau and my result:
Code: Select all
//add to project
unit global;

interface

uses
  System.Classes, IdHTTP, SysUtils, System.Notification;

///  <summary>
///  Процедура регистрации устройства.
///  </summary>
///  <param name="DeviceID">
///  ID регистрируемого устройства
///  </param>
///  <param name="DeviceToken">
///  Токен регистрируемого устройства
///  </param>
procedure RegisterDevice(DeviceID : string; DeviceToken : string);

///  <summary>
///  Процедура отправки Push сообщения на сервер.
///  </summary>
///  <param name="MessageText">
///  Текст отправляемого сообщения
///  </param>
procedure SendPush(MessageText : string);

///  <summary>
///  Процедура вывода сообщения из приложения.
///  </summary>
///  <param name="MessageText">
///  Текст выводимого сообщения
///  </param>
///  <param name="BadgeNumber">
///  Число выводимое на иконку приложения
///  </param>
procedure ShowNotification(MessageText : string; BadgeNumber : integer);

const
  // Доменное имя сайта
  DOMAIN: string = '193.106.248.114';

implementation


procedure RegisterDevice(DeviceID : string; DeviceToken : string);
var
  // Данные для передачи скрипту
  postdata: TStringList;
  // Подключение для передачи данных
  httpconnect : TIdHTTP;
begin

  try
    // Создаём подключение
    httpconnect := TIdHTTP.Create;
    // Указываем данные для отправки
    postdata := TStringList.Create;
    postdata.Add('action=register-device');
    postdata.Add('did=' + DeviceID);
    postdata.Add('token=' + DeviceToken);
    {$ifdef ANDROID}
      postdata.Add('platform=android');
    {$else}
      postdata.Add('platform=ios');
    {$endif}

    // Отправляем запрос
    httpconnect.Post(DOMAIN + 'push.php', postdata);
  finally
    // Отключаемся и освобождаем память
    httpconnect.Disconnect;
    httpconnect.DisposeOf;
  end;
end;

procedure SendPush(MessageText : string);
var
  // Данные для передачи скрипту
  postdata: TStringList;
  // Подключение для передачи данных
  httpconnect : TIdHTTP;
begin
  try
    // Создаём подключение
    httpconnect := TIdHTTP.Create;

    // Указываем данные для отправки
    postdata := TStringList.Create;
    postdata.Add('action=send-push');
    postdata.Add('text=' + MessageText);

    // Отправляем запрос
    httpconnect.Post(DOMAIN + 'push.php', postdata);
  finally
    // Отключаемся и освобождаем память
    httpconnect.Disconnect;
    httpconnect.DisposeOf;
  end;
end;

procedure ShowNotification(MessageText : string; BadgeNumber : integer);
var
  NotificationC: TNotificationCenter;
  Notification: TNotification;
begin

  // Создаём центр уведомлений и уведомление для отправки
  NotificationC := TNotificationCenter.Create(nil);
  Notification := NotificationC.CreateNotification;

  try
    // Если центр уведомлений поддерживается системой
    if NotificationC.Supported then
    begin
      // Устанавливаем текст сообщения
      Notification.AlertBody := MessageText;
      // Включаем звук при выводе сообщение
      Notification.EnableSound := true;
      // Устанавливаем цифру на иконке приложения
      Notification.Number := BadgeNumber;
      NotificationC.ApplicationIconBadgeNumber := BadgeNumber;
      // Выводим сообщение из приложения
      NotificationC.PresentNotification(Notification);
    end;
  finally
    // Очищаем переменные
    Notification.DisposeOf;
    NotificationC.Free;
    NotificationC.DisposeOf;
  end;
end;

end.


Code: Select all

    void __fastcall OnReceiveNotificationEvent(TObject *Sender, TPushServiceNotification* const ANotification);
    void __fastcall OnServiceConnectionChange(TObject *Sender, TPushService::TChanges AChange);


private:   // User declarations
     TPushService * APushService;
    TPushServiceConnection * AServiceConnection;


Code: Select all
#include "global.hpp"

const String FAndroidServerKey = L"875940064719";
void __fastcall TForm1::FormShow(TObject *Sender)
{
  APushService = nullptr;
  AServiceConnection = nullptr;

  String ADeviceID = "";
  String ADeviceToken = "";
  #if defined(__ANDROID__)
  APushService = TPushServiceManager::Instance->GetServiceByName(TPushService_TServiceNames_GCM);
  APushService->AppProps[TPushService_TAppPropNames_GCMAppID] = FAndroidServerKey;
  #endif
  #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
  APushService = TPushServiceManager::Instance->GetServiceByName(TPushService_TServiceNames_APS);
  #endif

  if (APushService)
    {
      AServiceConnection = new TPushServiceConnection(APushService);
      AServiceConnection->OnChange = &OnServiceConnectionChange;
      AServiceConnection->OnReceiveNotification = &OnReceiveNotificationEvent;
      AServiceConnection->Active = true;

      ADeviceID  = APushService->DeviceIDValue[TPushService_TDeviceIDNames_DeviceID];
      ADeviceToken = APushService->DeviceTokenValue[TPushService_TDeviceTokenNames_DeviceToken];

      if(ADeviceID != "" && ADeviceToken != "")
        {
         RegisterDevice(ADeviceID, ADeviceToken); //global.hpp
        }
   }

}
//---------------------------------------------------------------------------

void __fastcall TForm1::OnServiceConnectionChange(TObject *Sender, TPushService::TChanges AChange)
{
   if (AChange.Contains(TPushService::TChange::DeviceToken) && (AServiceConnection))
    {
        String ADeviceID = "";
        String ADeviceToken = "";
      ADeviceID = APushService->DeviceIDValue[TPushService_TDeviceIDNames_DeviceID];
      ADeviceToken = APushService->DeviceTokenValue[TPushService_TDeviceTokenNames_DeviceToken];

      if(ADeviceID != "" && ADeviceToken != "")
        {
         RegisterDevice(ADeviceID, ADeviceToken); //global.hpp
        }

   }

}
//---------------------------------------------------------------------------

void __fastcall TForm1::OnReceiveNotificationEvent(TObject *Sender, TPushServiceNotification* const ANotification)
{
    //ShowMessage(ANotification->Json->ToString());

   String MessageText = "";
   
   #if defined(__ANDROID__)
   MessageText = ANotification->DataObject->GetValue("message")->Value();
   #endif
   #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
   MessageText = ANotification->DataObject->GetValue("alert")->Value();
   #endif
   if(MessageText != "")
      ShowNotification(MessageText, 0);

}


On line:
APushService->AppProps[TPushService_TAppPropNames_GCMAppID] = FAndroidServerKey;
Error: segmentation fault 11

:(
Lena
BCBJ Master
BCBJ Master
 
Posts: 454
Joined: Sun Feb 06, 2011 1:28 pm

Re: The native push

Postby Lena » Fri Apr 07, 2017 7:30 am

Lena
BCBJ Master
BCBJ Master
 
Posts: 454
Joined: Sun Feb 06, 2011 1:28 pm

Re: The native push

Postby 2ffat » Fri Apr 07, 2017 10:10 am

Lena,
What are we voting for? Should this be in another thread?
James P. Cottingham

Look at me still talking
when there is science to do.
User avatar
2ffat
Forum Mod
Forum Mod
 
Posts: 426
Joined: Wed Jun 23, 2004 7:07 am
Location: South Hill, VA

Re: The native push

Postby Lena » Fri Apr 07, 2017 10:44 pm

2ffat wrote:Lena,
What are we voting for? Should this be in another thread?


Have you read this topic? I found the problem in this line:
APushService = TPushServiceManager::Instance->GetServiceByName(TPushService_TServiceNames_GCM); //NULL!!!

And then opened the vote.
Lena
BCBJ Master
BCBJ Master
 
Posts: 454
Joined: Sun Feb 06, 2011 1:28 pm

Re: The native push

Postby Lena » Sat Apr 08, 2017 10:40 am

Thank you all voted solution is found

Code: Select all
#if defined(__ANDROID__)
#include <FMX.PushNotification.Android.hpp>
// Workaround for RSP-17714
namespace Fmx {

 namespace Pushnotification {

  namespace Android {

   _INIT_UNIT(Fmx_Pushnotification_Android);

  }

 }

} // End-of-Workaround
#endif
Lena
BCBJ Master
BCBJ Master
 
Posts: 454
Joined: Sun Feb 06, 2011 1:28 pm


Return to Technical

Who is online

Users browsing this forum: No registered users and 7 guests

cron