Android Question How to push notification using Php ?

Adamdam

Active Member
Licensed User
Longtime User
Dear all,

Greetings,

I used Mr. Erel B4J-Push tool it worked well (thanks Erel)
Now, I need to make push notification using php.
I try to use "https://www.b4x.com/android/forum/threads/b4x-firebase-push-notifications-2023.148715/post-943080" from Mr. FrostCodes (it seam very attractive and important tool, thanks Mr. FrostCodes) but got the following error

bool(true) bool(true) string(60) "The registration token is not a valid FCM registration token" string(60) "The registration token is not a valid FCM registration token"

any help please to run php as pusher for notification, please
The above code (of Mr. FrostCodes) or any other php code.

Thanks on advance for nay help
Best regards
 

XorAndOr

Active Member
Licensed User
Longtime User
following error
bool(true) bool(true) string(60) "The registration token is not a valid FCM registration token" string(60) "The registration token is not a valid FCM registration token"
Comment the code at the bottom of the file (firebase.utils.php)
that code is just an example
PHP:
//FCM TOKEN EXAMPLES

//$sampleFcmUserToken = 'XXXXXXXXXXXXXXXX';
//$tokenTitle = 'TOKEN NOTIFICATION_TITLE @ '. date('H:i:s');
//$tokenMessage = 'TOKEN NOTIFICATION_BODY @  '. date('H:i:s');
//var_dump(sendNotificationToSingleRecipient($sampleFcmUserToken, $tokenTitle, $tokenMessage)); // No custom data
//var_dump(sendNotificationToSingleRecipient($sampleFcmUserToken, $tokenTitle, $tokenMessage, false, array('action' => 'no_action'))); // With custom data
 
Upvote 0

Adamdam

Active Member
Licensed User
Longtime User
Comment the code at the bottom of the file (firebase.utils.php)
that code is just an example
PHP:
//FCM TOKEN EXAMPLES

//$sampleFcmUserToken = 'XXXXXXXXXXXXXXXX';
//$tokenTitle = 'TOKEN NOTIFICATION_TITLE @ '. date('H:i:s');
//$tokenMessage = 'TOKEN NOTIFICATION_BODY @  '. date('H:i:s');
//var_dump(sendNotificationToSingleRecipient($sampleFcmUserToken, $tokenTitle, $tokenMessage)); // No custom data
//var_dump(sendNotificationToSingleRecipient($sampleFcmUserToken, $tokenTitle, $tokenMessage, false, array('action' => 'no_action'))); // With custom data
Thanks Sir.
I comment as you said, the result is changed to "bool(true) bool(true" but no notification received.
what is the problem ?
note : the B4JPush from Erel is work well.
 
Upvote 0

XorAndOr

Active Member
Licensed User
Longtime User
no notification received.
If this json file is the last one you use (I saw it in your previous posts)

pushertesting-418f9-firebase-adminsdk-vi0mo-171539421c.json

you have to rename it to ---> service_account.json

and put it in the server folder, where there are the php files
 
Upvote 0

Adamdam

Active Member
Licensed User
Longtime User
If this json file is the last one you use (I saw it in your previous posts)

pushertesting-418f9-firebase-adminsdk-vi0mo-171539421c.json

you have to rename it to ---> service_account.json

and put it in the server folder, where there are the php files
Thanks a lot for your help.

Yes, I change name as author recommended, and put it in the root with php file.
Please see attached folder.

Best regards
 

Attachments

  • Noti_2024.zip
    400.1 KB · Views: 19
Upvote 0

lucasheer

Active Member
Licensed User
Longtime User
PHP:
function sendNotification($title = "", $body = "", $topic=""){
    $authorization_key = "AAAAfwXXXX:XXXXXXXXXXXXXXX";

    $fb_key_array = ['usertoken1', 'usertoken2'];

    $arrayToSend = ['registration_ids' => $fb_key_array,
                    'notification' => ['title' =>$title , 'body' => $body, 'sound' => 'default'],
                    'priority' => 'high',
                    'data' => ['title' =>$title, 'body' => $body, 'topic'=>$topic]  ];
        
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://fcm.googleapis.com/fcm/send");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($arrayToSend));  //Post Fields
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: key='.$authorization_key));
    $server_output = curl_exec ($ch);

    curl_close ($ch);
}

Hello sir! This is what I use to send Firebase push notifications in PHP using curl.

I have the FB tokens tied to each user. This allows me to use my own data to query who I want to send to. I believe that you can not send 'registration_ids' and only send 'topic' to send to all users subscribed to that topic.

Thank you!!
 
Upvote 0

Andie

Member
Licensed User
Longtime User
Great!
Only a little detail: What do I have to change in these php files to send emojis?
I tried different commands like:
PHP:
$text_including_emojis = utf8_encode($text_including_emojis);

$text_including_emojis = mb_convert_encoding($text_including_emojis,'UTF-8', mb_list_encodings());
But no success! Instead of emojis only mysterious signs are displayed in my receiving B4X app
 
Upvote 0

Andie

Member
Licensed User
Longtime User
Thanks a lot for your answer.
I have to be a little bit more specific.

I send a notification via a B4X app in this way:
B4X:
j.Download2("http://mywebspace.com/firebase.utils.php", _
            Array As String("text", notification_text, "topic", "newMessage")

The string variable notification_text contains regular text as well as emojis which I create via:
B4X:
Sub emoji_UTS (codepoint As Int) As String
    Dim bc As ByteConverter
    Dim b() As Byte = bc.IntsToBytes(Array As Int(codepoint))
    '
    Return BytesToString(b, 0, 4, "UTF32")
End Sub

Within the firebase.utils.php file I have then the content of notification_text which I asign to a variable my_notifaction in this php script. And at that point I have do something with my_notification to display the emoji.
 
Upvote 0

Andie

Member
Licensed User
Longtime User
Shame on me! You are absolutely right: To do nothing is the solution. Thank you!!!! :)

As a little excuse I must mention that I had to do this utf-8 transformation in the old php version with the old cloud messaging api.
 
Upvote 0
Top