4

I have implemented push notifications for Chrome, and in the point when I need send push message to GCM, I used the following PHP function:

function send_push_message($subscription_ids){
  // Set GCM endpoint
  $url = 'https://android.googleapis.com/gcm/send';

  $fields = array(
      'registration_ids' => $subscription_ids,
  );

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

  $ch = curl_init();

  // Set the url, number of POST vars, POST data
  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_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

  // Execute post
  $result = curl_exec($ch);
  if ($result === FALSE) {
      die('Push msg send failed in curl: ' . curl_error($ch));
  }

  // Close connection
  curl_close($ch);
}

(I have stored the subscriptions previously)

I think that I can do something like this for Firefox, using this url: https://updates.push.services.mozilla.com/push

But I don't know what I have to do.

I need register my website with Mozilla to do that, like in Google?

Please help!

1 Answers1

3

My own solution, (a colleague has helped with my bug in the curl request to mozilla), is working now

function send_push_message($subscriptionIDs){

if (empty($subscriptionIDs)) return FALSE;
$chs = $sChrome = array();
$mh = curl_multi_init();
foreach ($subscriptionIDs as $subscription){
    $i = count($chs);
    switch ($subscription["browser"]){
        case "firefox":
            $chs[ $i ] = curl_init();
            curl_setopt($chs[ $i ], CURLOPT_URL, "https://updates.push.services.mozilla.com/push/".$subscription["id"] );
            curl_setopt($chs[ $i ], CURLOPT_PUT, TRUE);
            curl_setopt($chs[ $i ], CURLOPT_HTTPHEADER, array( "TTL: 86400" ) );
            curl_setopt($chs[ $i ], CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($chs[ $i ], CURLOPT_SSL_VERIFYPEER, FALSE);

            curl_multi_add_handle($mh, $chs[ $i ]);
        break;
        case "chrome":
            $sChrome[] = $subscription["id"];
        break;    
    }
}
if (!empty($sChrome)){
    $i = count($chs);
    $chs[ $i ] = curl_init();
    curl_setopt($chs[ $i ], CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
    curl_setopt($chs[ $i ], CURLOPT_POST, TRUE);
    curl_setopt($chs[ $i ], CURLOPT_HTTPHEADER, array( "Authorization: key=MY_KEY", "Content-Type: application/json" ) );
    curl_setopt($chs[ $i ], CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($chs[ $i ], CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($chs[ $i ], CURLOPT_POSTFIELDS, json_encode( array( "registration_ids" => $sChrome ) ) );
    curl_multi_add_handle($mh, $chs[ $i ]);
}

do {
    curl_multi_exec($mh, $running);
    curl_multi_select($mh);
} while ($running > 0);

for ($i = 0; $i < count($chs); $i++){
    curl_multi_remove_handle($mh, $chs[ $i ]);
}            

curl_multi_close($mh);
}

($subscriptionIDs is an array of arrays with 2 keys: id and browser)

  • 1
    Are you sure it is working for Firefox? I did the same but it did not work. What is the response format you get from Mozilla server when you send the request? – usef_ksa Feb 11 '16 at 12:15
  • Yes, is working now in my application. When I tested mozilla server via CURL (curl -X PUT https://updates.push.services.mozilla.com/push/-browser-endpoint-) and everything was OK, the server response was empty. – Wilder Batista González Feb 11 '16 at 12:28
  • @WilderBatistaGonzález, In Mozilla I am not able to send push notification, can u help me ? – Nikhil sHETH May 31 '16 at 12:00
  • Check the update of my solution, I have to send TTL header now to Mozilla servers. I hope that help you. – Wilder Batista González Jun 01 '16 at 15:42
  • how to send more than 1000 push notifications for both firefox and chrome? – clint Jul 20 '16 at 12:52
  • Hi, I have tried your provided solution for mozilla, but its not working. Do I need to do something with Google Cloud Message Key for mozilla or it is same for both . TIA – Jay Hardia Sep 29 '16 at 06:23
  • where is the notification body, title etc. – Sam Dec 15 '18 at 19:28