Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push to Specific User

Writing in Delphi 10 Seattle, targeting the 'pusher' as Windows, and the receiver as iOs (for now). The aim is to be able to push a message to a specific user without relying on broadcast and client side filtering.

I've managed to achieve the following so far: 1. Send a broadcast push to my iOS app 2. Login as a user on my iOS app 3. Create a pointer in my Installation record for User -> _User

That's as far as I can get in Delphi. From what I understand, I have to update the Installation record on login to reflect the installations logged in user.

I cannot find out how to do that in Delphi's Parse/BAAS objects. What seems to be missing is that I can't get the logged in users Installation ID. I assume if I could I could then update that via the TBackendStorage Class.

Any help would be appreciated.

I've cross posted this question to Embarcadero forums and community site.

like image 254
Christopher Burke Avatar asked Jun 18 '26 06:06

Christopher Burke


1 Answers

To retrieve the User Object Id

var
  ACreatedObject: TBackendEntityValue; // REST.Backend.MetaTypes
begin
  BackendUsers.Users.LoginUser('donald','#duck99',ACreatedObject);
  fUserObjectId:=ACreatedObject.ObjectID;
end

To Update the Installation with the User Object iD

Assuming you have a column 'User' in your Installation table on Parse as a pointer to _User class.

Within the PushEventsDeviceRegistered Event:

var
  JO,JOP:TJSonObject;     // System.JSON
  O:TBackendEntityValue;  // REST.Backend.MetaTypes
begin
  if PushEvents.InstallationValue.TryGetObjectID(fInstallationObjectId) then
  begin
    try
      JO:=TJSONObject.Create;
      JOP:=TJSONObject.Create;
      JOP.AddPair('__type','Pointer');
      JOP.AddPair('className','_User');
      JOP.AddPair('objectId',fUserObjectId);
      JO.AddPair('User',JOP);
      BackendStorage.Storage.UpdateObject('_Installation',
                            fInstallationObjectId,JO,O);
    finally
      JO.Free;
    end;
  end
end; 

To Target the Push Message Based on User Name

Note, you could create the target via JSON objects,but I've used a string with formatting here.

const
  Target = '{"where":{"User":{"$select":{"query":'+
           '{"__type":"Pointer","className":"_User","where":'+          
           '{"username":"%s"}},"key":"objectId"}}}}';
begin
  BackendPush.Target.Text:=Format(Target,['donald']);
  BackendPush.Message:='Gratz on the Election Result';
  BackendPush.Push;
end

Non Local Variables/Declarations

The following Delphi BaaS components were created at Design time.

ParseProvider: TParseProvider;
PushEvents: TPushEvents;
BackendUsers: TBackendUsers;
BackendStorage: TBackendStorage;
BackendPush: TBackendPush;

The following class (or global) variables are referred to:

fUserObjectId:string; // Must be set before push registration is activated.
fInstallationObjectId:string;

NOTE: The original code is fully tested, however I've cut/paste (and edited to remove non-relevant stuff) so forgive me if there are any cut/paste errors.

like image 150
Christopher Burke Avatar answered Jun 19 '26 19:06

Christopher Burke