Ionic phase notification using PHP does not work

I want to send notification to my Ionic 2 app using firebase. I can send notification directly using firebase console, but I want to send it via php file.

when i post i get a response from PHP as: {"message_id":5718309985299480645}

And there are no notifications in the phone.

I placed this.fcm.subscribeToTopic('all')

in the constructor app.component.ts.

I don't know what I am doing wrong.

this.fcm.subscribeToTopic('all')

is the only fcm related code in my application.

MY PHP CODE:

<?php 

   $data = array('title'=>'FCM Push Notifications');
   $target = '/topics/mytopic';


   //FCM API end-point
   $url = 'https://fcm.googleapis.com/fcm/send';
   //api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
   $server_key = 'my_server_key_from_firebase';

   $fields = array();
   $fields['data'] = $data;
   if(is_array($target)){
   $fields['registration_ids'] = $target;
   }else{
   $fields['to'] = $target;
   }

   //header with content_type api key
   $headers = array(
   'Content-Type:application/json',
   'Authorization:key='.$server_key
   );
   //CURL request to route notification to FCM connection server (provided by Google)           
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
   $result = curl_exec($ch);
   if ($result === FALSE) {
   die('Oops! FCM Send Error: ' . curl_error($ch));
   }
   curl_close($ch);
   echo $result;

?>

      

I MUST PLAY A PATH BUT CANNOT GET NIGHT ON DEVICE WITH PHP

+3


source to share


2 answers


I solved the problem by looking at the fcm docs .

I changed the PHP file to:



   $data = array('title'=>'Title of the notification', 'body'=>$msg, 'sound'=>'default');
   $target = '/topics/notetoall';

   $url = 'https://fcm.googleapis.com/fcm/send';

   $server_key = 'my_server_api_key_from_firebase';

   $fields = array();
   $fields['notification'] = $data; // <-- this is what I changed from $fields['body']
   if(is_array($target)){
   $fields['registration_ids'] = $target;
   }else{
   $fields['to'] = $target;
   }

      

+3


source


All plugins should be called after the device is ready, especially if they are used in app.components

(on some pages that are not the first, it can be used inside the constructor, since the application is already ready and the plugins are loaded).

So, subscribe to the finished device topic



this.platform.ready().then(() => {
  this.fcm.subscribeToTopic('all')
});

      

Hope it helps.

+1


source







All Articles