繁体   English   中英

如何使用 php 在 android studio 中使用 GCM 实现推送通知?

[英]how to implement push notification using GCM in android studio using php?

我正在尝试使用 GCM 在我的应用程序中获取通知。 我正在使用 android studio 工具,我尝试了很多教程。 其中大部分是使用 eclips 工具完成的。 最后,我尝试了这个,
http://rmarcejaeger.com/2015/09/18/tutorial-how-to-implement-push-notifications-using-google-cloud-messaging-for-android-part-1-client-app/#comment-576070 , https://github.com/googlesamples/google-services/tree/master/android/gcm

我使用上面的教程完成了 android 客户端部分。 但问题是,我需要在 php 中设置应用服务器,它通过 GCM 连接我的应用程序。 我不知道使它成为可能。 我尝试了以下链接, https://developers.google.com/cloud-messaging/server

但我不能得到更多。所以请任何人建议我更好的解决方案。

首先,您需要从控制台创建浏览器密钥。

这是您需要为发送通知添加的代码 GOOGLE_API_KEY 是您从控制台创建的密钥。

 <?php class GCM { //put your code here // constructor function __construct() { } /** * Sending Push Notification */ public function send_notification($registatoin_ids, $message) { // include config include_once './config.php'; // Set POST variables $url = 'https://android.googleapis.com/gcm/send'; $fields = array( 'registration_ids' => $registatoin_ids, 'data' => $message, ); $headers = array( 'Authorization: key=' . GOOGLE_API_KEY, 'Content-Type: application/json' ); // Open connection $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); // Disabling SSL Certificate support temporarly 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('Curl failed: ' . curl_error($ch)); } // Close connection curl_close($ch); echo $result; } } ?>

现在使用 GCM 类的这个方法,您可以通过调用其方法向设备发送通知。 传递 reg.id 这是您从 Android 设备获得的令牌 ID 和您想要传递的消息。

 $gcm = new GCM(); $result = $gcm->send_notification($registatoin_ids, $message); echo $result;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM