Invio Push Notification su più dispositivi è la stessa come trasmettiamo dispositivo individuo. Basta memorizzare il token di registrazione di tutti i dispositivi registrati sul server. E quando si chiama la notifica push con arricciatura (presumo che tu stia usando php come lato server) Metti tutto l'ID di registrazione in un array. Si tratta di un codice di esempio
<?php
//Define your GCM server key here
define('API_ACCESS_KEY', 'your server api key');
//Function to send push notification to all
function sendToAll($message)
{
$db = new DbOperation();
$tokens = $db->getAllToken();
$regTokens = array();
while($row = $tokens->fetch_assoc()){
array_push($regTokens,$row['token']);
}
sendNotification($regTokens,$message);
}
//function to send push notification to an individual
function sendToOne($email,$message){
$db = new DbOperation();
$token = $db->getIndividualToken($email);
sendNotification(array($token),$message);
}
//This function will actually send the notification
function sendNotification($registrationIds, $message)
{
$msg = array
(
'message' => $message,
'title' => 'Android Push Notification using Google Cloud Messaging',
'subtitle' => 'www.simplifiedcoding.net',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/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);
$res = json_decode($result);
$flag = $res->success;
if($flag >= 1){
header('Location: index.php?success');
}else{
header('Location: index.php?failure');
}
}
Nel codice di cui sopra cercando di caricare il token di registrazione dalla tabella mysql. Per inviare a tutti i dispositivi abbiamo bisogno di tutti i token. E per inviare un singolo dispositivo abbiamo bisogno di token solo per quel dispositivo.
Fonte: Google Cloud Messaging Example
fonte
2016-04-21 15:00:01
possibile duplicato di [fare vecchi gettoni GCM vivono anche dopo una disinstallazione?] (http://stackoverflow.com/questions/17328654/do-old-gcm- token-live-on-anche-dopo-an-disinstallazione) –