利用Google GCM发送push通知到Android客户端

// 这个可以需要在google账号中申请,勾选gcm服务选项
$apiKey = 'AIzaSyC6h3ysrn2HDCBqONTo2vKIVVuktIFoxxx'; $headers = array(
"Content-Type:" . "application/json",
"Authorization:" . "key=" . $apiKey
); $messageData = array(
'type' => 1,
'msg' => 'news test',
'push_id' => 100
); // device token,Android客户端获取到的,注册的时候保存到数据库中
// 连接一次可以推送到多个客户端,这里建议最大1000条
$regid = array(
'APA91bEapIMawO4Kg5tLul__sKVtWItS6j7vdvpjEw1bv1dDCHSzYtucz0PCGdtdAASL1bQdVTgmVCIN4I8u9HZ8fnUqpM3jwwy4DwVXHpZmEr4NO18ORJlIox97BUun_fgZzYBWWoSxJY_H5PJHsi0WoWZcYDgSLQ',
'APA91bHY-F_U-ovEMRM_hRJegcD7QDxxffh4uNjiaBIIBTD1mVUc2BSqSdhvTPnhrMhN-E3msNganWaYN7s62UzqTrZYNGJl29lbTkHWWUzAywwK1V99LYK4ICn_TQfwciftuNwqlgJNOPrszYf5yLlKTzKpOUG3OQ',
'APA91bHHeGuCPz7WH6Kc9dPhiV67aSfQ1WOG7fK5N18msOW5EhATeqAFhyvHPxGC3ANKkgGFn0AbQi3r3KF6yKALGJqRPJXJ_HALhCCUPRiKXYjIkpiuMyP1pb6xZNaWETzHYDsWPRkgoeSnGcX19RVGWHs2JJOQWQ',
'APA91bE6HyQV19QOk6SgfQnmeIa2d47-i82dxtPRrGCe9MXjiFI-nYglgS_6hOaMb7TQRssQ9M0DHkHdUDYygDZe3tbt_zymRXDCFa6By1LcTZPNUDNCDvP3h-yqt3j1YZIMwvSOFTqNLJ9GgrjM54RNao5P2q6E2g',
'APA91bENGEtKY8rz9r3CcT82et62kvKEzzdMtww1PUKmf3VOK3bh_O4QaK9PSrUzTZwd15CHkEAbIMi_9mP3J0BnrJox1NqO25OoOZ5kkOlPpUKPvm7byfTGgQRsITkbS_TpP2Wg8A3bONEWR9CBjmOfyWZjImBFXA',
'APA91bFeBqgGqeMAbmab62xocPf1RejGJmQBEKjTFvEk7HurL5A7zVeQ-0-7v6ekQ6rYehgr8AZza5cEE1iZSJt47HvFgmAFqID7YeiAsO7wYW9en0xM4GLnJgGGSfy6VNbqogsP6ftIxVFuKeHzzcJb8u1IBCzZGw',
); $data = array(
'data' => $messageData,
'registration_ids' => $regid
); $url = "https://android.googleapis.com/gcm/send";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $response = curl_exec($ch);
curl_close($ch); var_dump($response);

创建GOOGLE项目的方法

1、创建Google项目https://cloud.google.com/console/project
2、开启GCM 菜单栏依次找到"APIs & auth –> APIs –> Google Cloud Messaging For Android" 打开这个选项 "ON"
3、创建server key 找到"Credentials"菜单 点击server key按钮创建
4、编辑允许访问的IP,可以不设定

这里就有了发送GOOGLE GCM的key了,更详细的可以参照

http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/

上一篇:【spring boot】在Spring mvc中controller中可以拿到对象信息,但是返回给前台却是什么也没有,解决方案


下一篇:Android端高性能图像分类解决方案