Delphi Xe8 , How to get android device token?

1.7k views Asked by At

I registered Google Cloud Messaging(GCM) system. I am using Delphi Xe8 .

I need to get android device token for send notification with gcm.

But I have no idea about device token.

How can I get device token ?

3

There are 3 answers

0
mwroh On BEST ANSWER

You can get the DeviceID and DeviceToken using follow code...

var 

    ADeviceID, ADeviceToken : String; 

begin

    APushService := TPushServiceManager.Instance.GetServiceByName( TPushService.TServiceNames.GCM );
    APushService.AppProps[ TPushService.TAppPropNames.GCMAppID ] := '123...GCMAppID...456';  // Your GCM App ID
    AServiceConnection := TPushServiceConnection.Create( APushService );
    AServiceConnection.Active   := True;
    AServiceConnection.OnChange := OnServiceConnectionChange;
    AServiceConnection.OnReceiveNotification := OnReceiveNotificationEvent;
    ADeviceID    := APushService.DeviceIDValue[ TPushService.TDeviceIDNames.DeviceID ];
    ADeviceToken := APushService.DeviceTokenValue[ TPushService.TDeviceTokenNames.DeviceToken ];
end;
0
shyambabu On
  p : TJavaObjectArray<JString>;
begin
  gcm :=   TJGoogleCloudMessaging.JavaClass.getInstance(SharedActivity.getApplicationContext);
  p := TJavaObjectArray<JString>.Create(1);
  p.Items[0] := StringToJString('GCM Project Id');
  Memo1.Lines.Add(JStringToString(gcm.register(p)));
end;
0
Kajisensi On

If you are following this manual http://docwiki.embarcadero.com/RADStudio/XE8/en/Multi-Device_Application_to_Receive_Push_Notifications

Then at the point on this manual where you make events from PushEvents1 You can get Devicetoken by this code.

procedure TForm1.PushEvents1DeviceTokenReceived(Sender: TObject);
begin
ShowMessage('Devicetoken received');
ShowMessage(PushEvents1.BindSource.Adapter.PushSender.DeviceToken);
end;