Recent

Author Topic: Free Pascal and sending push-notifications via FCM  (Read 4449 times)

JohnvdWaeter

  • Full Member
  • ***
  • Posts: 185
    • http://www.jvdw.nl
Re: Free Pascal and sending push-notifications via FCM
« Reply #15 on: May 09, 2024, 10:30:20 am »
After resolving all the dependencies it works! Thanks!
Well, for now in Windows :)

Just one little question: where should I insert "sound":"true" in the message?

I tried the defaultsound in the fpfcmtypes, but it had no effect.

rvk

  • Hero Member
  • *****
  • Posts: 6585
Re: Free Pascal and sending push-notifications via FCM
« Reply #16 on: May 09, 2024, 11:03:49 am »
For Apple, the sound object needs to be part of an aps object (in payload).
For Android it can be in notification.

See an example here
https://stackoverflow.com/a/48690940/1037511
« Last Edit: May 09, 2024, 11:07:00 am by rvk »

JohnvdWaeter

  • Full Member
  • ***
  • Posts: 185
    • http://www.jvdw.nl
Re: Free Pascal and sending push-notifications via FCM
« Reply #17 on: May 11, 2024, 01:08:12 pm »
Ok, got that (sound for apple) working too....

Last (I think) problem is UTF8. If I insert --body="bóçdy", The client receives "b dy" (without the quotes).

In fpfcmclient, line 225, I added utf8encode() ,
Code: Pascal  [Select][+][-]
  1. Request.SetContentFromString(utf8encode(AJSON));   //<<-- added utf8encode
and then the text appeared correctly in the push notification.

But I'm not sure if this is the right way or right place to do it....

Edit: it was not. The cause was title and body not surviving the passing via a commandline. Of course not :)
I ended up base64 encoding the title and the body and decoding it back in the sendmsg. All works great now, thanks RVK! :)
« Last Edit: May 13, 2024, 12:47:13 pm by JohnvdWaeter »

JohnvdWaeter

  • Full Member
  • ***
  • Posts: 185
    • http://www.jvdw.nl
Re: Free Pascal and sending push-notifications via FCM
« Reply #18 on: October 21, 2024, 11:00:35 am »
Hi RVK,
Still one question... where should I insert {"badge":"1"} in the JSON? I tried several places, but no effect...

rvk

  • Hero Member
  • *****
  • Posts: 6585
Re: Free Pascal and sending push-notifications via FCM
« Reply #19 on: October 21, 2024, 12:01:35 pm »
Hi RVK,
Still one question... where should I insert {"badge":"1"} in the JSON? I tried several places, but no effect...
Where did you find the documentation for this?

If you are using https://fcm.googleapis.com/v1/ shouldn't it be notification_count then?

https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages

(It also needs to be supported by the receiving program.)

Edit:
BTW. It's really important how you construct your message.
If you are going to use the notification section, you need to add 'android' etc for the special fields.
For example.. this should work for if you add the notification_count to 'android' subsection notification (it does for me):

Code: Text  [Select][+][-]
  1. {
  2.     "message": {
  3.         "notification": {
  4.             "title": "hello",
  5.             "body": "mememe"
  6.         },
  7.         "android": {
  8.             "notification": {
  9.                 "click_action": "OPEN_ACTIVITY_1",
  10.                 "tag": "foo",
  11.                 "notification_count": 13
  12.             }
  13.         },
  14.         "token": "xx",
  15.     }
  16. }
« Last Edit: October 21, 2024, 01:26:10 pm by rvk »

JohnvdWaeter

  • Full Member
  • ***
  • Posts: 185
    • http://www.jvdw.nl
Re: Free Pascal and sending push-notifications via FCM
« Reply #20 on: October 21, 2024, 02:24:56 pm »
Yes, I have it working okay on Android.

It's the apns/aps-part of the message:
Code: Text  [Select][+][-]
  1. {
  2.    "aps" : {
  3.       "badge" : 9,
  4.       "sound" : "bingbong.aiff"
  5.    },
  6.    "messageID" : "ABCDEFGHIJ"
  7. }

How to get the "badge":"1" there?

rvk

  • Hero Member
  • *****
  • Posts: 6585
Re: Free Pascal and sending push-notifications via FCM
« Reply #21 on: October 21, 2024, 02:39:48 pm »
It's the apns/aps-part of the message:
[]
How to get the "badge":"1" there?
I'm not familiar with the apns/aps-part (Apple).

Did you wrap the apns in a payload key?
Like is mentioned here:
https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#ApnsConfig


JohnvdWaeter

  • Full Member
  • ***
  • Posts: 185
    • http://www.jvdw.nl
Re: Free Pascal and sending push-notifications via FCM
« Reply #22 on: October 21, 2024, 04:01:56 pm »
I managed to get a badge number 1 with adding 1 line of code in unit fpfcmtypes:

In procedure AddPayLoad, there is a line
 
Code: Pascal  [Select][+][-]
  1.  soundobj.Add('sound','default');

I added one line below it:
Code: Pascal  [Select][+][-]
  1.  soundobj.Add('sound','default');
  2.  soundobj.Add('badge',1);  
Now I have my badge number on iOS :)

rvk

  • Hero Member
  • *****
  • Posts: 6585
Re: Free Pascal and sending push-notifications via FCM
« Reply #23 on: October 21, 2024, 04:06:35 pm »
I managed to get a badge number 1 with adding 1 line of code in unit fpfcmtypes:

In procedure AddPayLoad, there is a line
 
Code: Pascal  [Select][+][-]
  1.  soundobj.Add('sound','default');

I added one line below it:
Code: Pascal  [Select][+][-]
  1.  soundobj.Add('sound','default');
  2.  soundobj.Add('badge',1);  
Now I have my badge number on iOS :)
Ah, you are using fpfcmtypes (and the Google suite) in FPC.
I've never used that.

But in fpfcptypes.pp I also see this (in TAppleConfig.AddPayLoad):
Code: Pascal  [Select][+][-]
  1. begin
  2.   if (AlertObject.Count>0) then
  3.     Obj.Add('alert',AlertObject.Clone)
  4.   else
  5.     JSONAdd(Obj,'alert',Alert);
  6.   JSONAdd(Obj,'badge',Badge);
  7.   JSONAdd(Obj,'sound',Sound);
  8.   JSONAdd(Obj,'thread-id',ThreadID);

Isn't "badge" already there then?
Shouldn't you just assign 1 to component.Badge to make it add the badge tag?

Or do you have older source where this line isn't present?

Code: Pascal  [Select][+][-]
  1.   TAppleConfig = class(THeadersPlatformConfig)
  2.   private
  3.     FAlert: string;
  4.     FAlertObject: TJSONObject;
  5. //...
  6.   Published
  7.     Property Alert : string Read FAlert Write falert;
  8.     property Badge : Integer Read FBadge write fbadge;
  9.     Property ThreadID : string read Fthreadid Write Fthreadid;

I guess you are working with an older version of FPC.
« Last Edit: October 21, 2024, 04:10:10 pm by rvk »

JohnvdWaeter

  • Full Member
  • ***
  • Posts: 185
    • http://www.jvdw.nl
Re: Free Pascal and sending push-notifications via FCM
« Reply #24 on: October 21, 2024, 04:22:37 pm »
Quote
I guess you are working with an older version of FPC.
Yes, I guess so..  3.2.0 on linux.
I've had so many headaches updating....
But yes, the line
 
Code: Pascal  [Select][+][-]
  1. JSONAdd(Obj,'badge',Badge);
is there, and I tried replacing it with JSONAdd(Obj,'badge',1);
but then it didn't appear in the aps-key.
Now it appears where it should:
Code: Javascript  [Select][+][-]
  1. ...
  2.  "apns":{
  3.       "payload":{
  4.         "aps":{
  5.           "sound":"default",
  6.           "badge":1,
  7.         }
  8.       }
  9. }
  10. ...


rvk

  • Hero Member
  • *****
  • Posts: 6585
Re: Free Pascal and sending push-notifications via FCM
« Reply #25 on: October 21, 2024, 04:40:05 pm »
I've had so many headaches updating....
But yes, the line
 
Code: Pascal  [Select][+][-]
  1. JSONAdd(Obj,'badge',Badge);
is there, and I tried replacing it with JSONAdd(Obj,'badge',1);
but then it didn't appear in the aps-key.
But is that line is there, the 'badge' should be added if you use it correctly.
So assigning 1 to TAppleConfig.Badge should do it.

So... for the TNotificationMessage that should be
Code: Pascal  [Select][+][-]
  1. NotificationMessage.AppleConfig.Badge := 1;

But you haven't shown any code how you are using fpfcmtypes.

Adding badge hardcoded as 1 is just a 'hack'  ;D

JohnvdWaeter

  • Full Member
  • ***
  • Posts: 185
    • http://www.jvdw.nl
Re: Free Pascal and sending push-notifications via FCM
« Reply #26 on: October 21, 2024, 04:48:23 pm »
Quote
Adding badge hardcoded as 1 is just a 'hack'
 

Yes :)  Whenever I have the time, I will study decent coding... for now I have to focus on getting things done.... Thanks again for your patience! :)

 

TinyPortal © 2005-2018