【问题标题】:Sending messages(notification) to a group - Android向群组发送消息(通知) - Android
【发布时间】:2014-04-22 06:10:35
【问题描述】:

我正在编写一个 Android 应用程序,我需要在其中向一组用户发送通知。

我在数据库中有两组用户。如果用户在 android 应用程序中按下“通知组 1”按钮,我需要将通知发送给所有组 1 用户。我如何实现这个逻辑?我认为在群聊中使用的是相同的逻辑。

能否提供安卓和服务器端的示例代码?

谢谢.. 撒迦利亚

【问题讨论】:

    标签: java php android mysql push-notification


    【解决方案1】:

    使用Android Cloud to Device Messaging 是最好的方法,因为您可以轻松地将它与 MySQL 和 PhP 集成,并拥有通过 Internet 发送消息的必要工具。

    您可以根据需要对用户进行分组:

     CREATE TABLE IF NOT EXISTS `gcm_users` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `gcm_regid` text,
      `name` varchar(50) NOT NULL,
      `email` varchar(255) NOT NULL,
      `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
    

    来源 - AndroidHive

    这里有一个 Java 类的典型示例,它使用 Google cloud 通过 Internet 发送消息:

    public class MessageUtil {
          private final static String AUTH = "authentication";
    
          private static final String UPDATE_CLIENT_AUTH = "Update-Client-Auth";
    
          public static final String PARAM_REGISTRATION_ID = "registration_id";
    
          public static final String PARAM_DELAY_WHILE_IDLE = "delay_while_idle";
    
          public static final String PARAM_COLLAPSE_KEY = "collapse_key";
    
          private static final String UTF8 = "UTF-8";
    
          public static int sendMessage(String auth_token, String registrationId,
              String message) throws IOException {
    
            StringBuilder postDataBuilder = new StringBuilder();
            postDataBuilder.append(PARAM_REGISTRATION_ID).append("=")
                .append(registrationId);
            postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=")
                .append("0");
            postDataBuilder.append("&").append("data.payload").append("=")
                .append(URLEncoder.encode(message, UTF8));
    
            byte[] postData = postDataBuilder.toString().getBytes(UTF8);
    
            // Hit the dm URL.
    
            URL url = new URL("https://android.clients.google.com/c2dm/send");
            HttpsURLConnection
                .setDefaultHostnameVerifier(new CustomizedHostnameVerifier());
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded;charset=UTF-8");
            conn.setRequestProperty("Content-Length",
                Integer.toString(postData.length));
            conn.setRequestProperty("Authorization", "GoogleLogin auth="
                + auth_token);
    
            OutputStream out = conn.getOutputStream();
            out.write(postData);
            out.close();
    
            int responseCode = conn.getResponseCode();
            return responseCode;
          }
    
          private static class CustomizedHostnameVerifier implements HostnameVerifier {
            public boolean verify(String hostname, SSLSession session) {
              return true;
            }
          }
     } 
    

    总结一下,您可以将使用 MySQL 的查询与 PhP 集成以实现您想要的。在这里你有PhP example的服务器端:

    send_message.php
    <?php
    if (isset($_GET["regId"]) && isset($_GET["message"])) {
        $regId = $_GET["regId"];
        $message = $_GET["message"];
    
        include_once './GCM.php';
    
        $gcm = new GCM();
    
        $registatoin_ids = array($regId);
        $message = array("price" => $message);
    
        $result = $gcm->send_notification($registatoin_ids, $message);
    
        echo $result;
    }
    ?>
    

    您可以在此处找到一些可能会用到的其他示例,包括系统逻辑:

    从这里开始,您可以获得具有您心目中的功能的应用程序。

    【讨论】:

    • 我需要通过互联网而不是短信发送消息......而且用户列表也在服务器数据库中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 2015-06-01
    • 2014-07-23
    • 2021-12-31
    • 2021-10-07
    相关资源
    最近更新 更多