Нотификация GCM & APNS (Google Cloud Messaging GCM)
Java
package com.javapapers.java.gcm;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;
@WebServlet("/GCMNotification")
public class GCMNotification extends HttpServlet {
private static final long serialVersionUID = 1L;
// Put your Google API Server Key here
private static final String GOOGLE_SERVER_KEY = "AIzaSyDA5dlLInMWVsJEUTIHV0u7maB82MCsZbU";
static final String MESSAGE_KEY = "message";
public GCMNotification() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Result result = null;
String share = request.getParameter("shareRegId");
// GCM RedgId of Android device to send push notification
String regId = "";
if (share != null && !share.isEmpty()) {
regId = request.getParameter("regId");
PrintWriter writer = new PrintWriter("GCMRegId.txt");
writer.println(regId);
writer.close();
request.setAttribute("pushStatus", "GCM RegId Received.");
request.getRequestDispatcher("index.jsp")
.forward(request, response);
} else {
try {
BufferedReader br = new BufferedReader(new FileReader(
"GCMRegId.txt"));
regId = br.readLine();
br.close();
String userMessage = request.getParameter("message");
Sender sender = new Sender(GOOGLE_SERVER_KEY);
Message message = new Message.Builder().timeToLive(30)
.delayWhileIdle(true).addData(MESSAGE_KEY, userMessage).build();
System.out.println("regId: " + regId);
result = sender.send(message, regId, 1);
request.setAttribute("pushStatus", result.toString());
} catch (IOException ioe) {
ioe.printStackTrace();
request.setAttribute("pushStatus",
"RegId required: " + ioe.toString());
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("pushStatus", e.toString());
}
request.getRequestDispatcher("index.jsp")
.forward(request, response);
}
}
}
PHP
GCM
function push_gcm($registrationIds) {
$apiAccessKey = 'AI.............b0';
$action = "send_coordinate";
$msg = array
(
'message' => '',
'action' => $action
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . $apiAccessKey,
'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, JSON_UNESCAPED_UNICODE));
curl_exec($ch);
curl_close($ch);
}
APNS
function push_apns($device_tokens) {
$is_dev = false;
$action = "send_coordinate";
$aps = array(
'content-available' => 1,
'sound' => ''
);
$body = array(
'aps' => $aps,
'action' => $action
);
$payload = json_encode($body);
$apns_port = 2195;
$pathdir = "/home/push-private/";
if ($is_dev) {
$apns_url = 'gateway.sandbox.push.apple.com';
$apns_cert = $pathdir . 'evak_driver_dev.pem';
$passphrase = "geel_evak_driver";
}
else {
$apns_url = 'gateway.push.apple.com';
$apns_cert = $pathdir . 'evak_driver_prod.pem';
$passphrase = "geel_evak_driver";
}
$stream_context = stream_context_create();
stream_context_set_option($stream_context, 'ssl', 'local_cert', $apns_cert);
stream_context_set_option($stream_context, 'ssl', 'passphrase', $passphrase);
$apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $stream_context);
if ($apns) {
foreach($device_tokens as $token) {
$apns_message = chr(0) . pack('n',32) . pack('H*', $token) . pack('n',strlen($payload)) . $payload;
fwrite($apns, $apns_message, strlen($apns_message));
}
@socket_close($apns);
@fclose($apns);
}
}
Статьи