Android Question Firebase push Notifications Php stopped working

tufanv

Expert
Licensed User
Hello,

For the last 2 years I am using the below function in my php to send messages successfully. Today I realized that none of my android apps receiving the messages sent via this code, messages sent directly from firebase console are receiving. the code is:

B4X:
function notify($r, $t, $m) {
  // API access key from Google API's Console
  if (!defined('API_ACCESS_KEY')) define( 'API_ACCESS_KEY', 'xxxxxxxx');
  $tokenarray = array($r);
  // prep the bundle
  $msg = array(
    'title'     => $t,
    'body'         => $m,
    'MyKey1'        => 'MyData1',
    'MyKey2'    => 'MyData2',
  );

  $fields = array(
    'registration_ids'    => $tokenarray,
    'data'                            => $msg
  );

  $headers = array(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
  );

  $ch = curl_init();
  curl_setopt( $ch,CURLOPT_URL, 'fcm.googleapis.com/fcm/send' );
  curl_setopt( $ch,CURLOPT_POST, true );
  curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
  curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
  curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
  $result = curl_exec($ch );
  curl_close( $ch );
  return $result;
}
is stg changed I am unaware ?
 

sorex

Expert
Licensed User
maybe it bumps out on the $msg array that's expecting a next value but it's not declared? ( 'MyData2', )
 

Brandsum

Well-Known Member
Licensed User
Try this tested code for php firebase push.
Android:
PHP:
        $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
        $apikey = 'xxx';
        $data = [
            'activity' =>$app_push->activity,
            'image' =>$app_push->image,
            'subText' => $app_push->subtext,
            'title' =>$app_push->title,
            'body' => $app_push->body
        ];
        $fcmNotification = [
            'to' => '/topics/AppPushMessage',
            'data' => $data
        ];

        $headers = [
            'Authorization: key=' . $apikey,
            'Content-Type: application/json'
        ];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$fcmUrl);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
        $result = curl_exec($ch);
        curl_close($ch);
iOS:
PHP:
        $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
        $apikey = 'xxx';

        $data = [
            'attachment-url' =>$app_push->image,
            'subText' => $app_push->subtext,
            'title' =>$app_push->title,
            'body' => $app_push->body
        ];
        $fcmNotification = [
            'priority'=>'high',
            "content_available"=> true,
            "mutable_content"=> true,
            'to' => '/topics/ios_AppPushMessage',
            "notification" => array(
              "body" =>  $app_push->body,
              "title" => $app_push->title,
              "subtitle" => $app_push->subtext,
              'sound' => 'default',
              'badge' => '0'
             ),
            'data' => $data
        ];
       
        $headers = [
            'Authorization: key=' . $apikey,
            'Content-Type: application/json'
        ];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$fcmUrl);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
        $result = curl_exec($ch);
        curl_close($ch);
 
Top