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: 418
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: 418
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: 1376
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: 418
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: 418
Joined: Sun Feb 06, 2011 1:28 pm


Return to Technical

Who is online

Users browsing this forum: No registered users and 8 guests

cron