Advertisement
Alonso_Fonseca

Tutorial fcm php

Oct 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.23 KB | None | 0 0
  1. $serverKey = 'Chave da sua api Firebase';
  2.                             $msg_notificacao = "Mensagem da notificacao";
  3.                    
  4.                             $pushUrl    = "https://fcm.googleapis.com/fcm/send";
  5.                             $key        = $serverKey;
  6.                             $headers    = [];
  7.  
  8.                             $headers[] = 'Content-Type: application/json';
  9.                             $headers[] = 'Authorization:key=' . $key;
  10.                             $data  = array(
  11.                                 'registration_ids' => //'Aqui vai um array com os tokens fcm dos usuarios que irao receber essa notificacao',
  12.                                 "notification" => array(
  13.                                         "title"         =>  "Titulo da sua notificacao",
  14.                                         "body"          =>  $msg_notificacao,
  15.                                         "click_action"  =>"FCM_PLUGIN_ACTIVITY",
  16.                                         "icon"          => "fcm_push_icon",
  17.                                         "priority" => "high",
  18.                                         "show_in_foreground" => true,
  19.                                         "targetScreen" => 'detail'
  20.                                 ),
  21.                                 "data" => array(
  22.                                         "notification" => array(
  23.                                             "title" =>  "Titulo da sua notificacao",
  24.                                             "body" =>  $msg_notificacao,
  25.                                             "click_action" =>"FCM_PLUGIN_ACTIVITY",
  26.                                             "icon" => "fcm_push_icon",
  27.                                             "priority" => "high",
  28.                                             "show_in_foreground" => true,
  29.                                             "targetScreen" => 'detail',
  30.                                             //dados de seu backend que queira mandar para o ionic esses 3 abaixos
  31.                                             "dia" => "Convite para um jogo",
  32.                                             "email_cancelou" => 'blablabla',
  33.                                             "aviso" => "sim",
  34.                                            
  35.                                         )
  36.                                 ),
  37.                                 "priority" => 10,
  38.                             );
  39.                             $json_data = json_encode($data);
  40.    
  41.                             $ch = curl_init();
  42.                             curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
  43.                             curl_setopt($ch, CURLOPT_POST, true);
  44.                             curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
  45.                             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  46.                             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  47.                             curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
  48.  
  49.                             $res = curl_exec($ch);
  50.                             curl_close($ch);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement