【问题标题】:send device to device gcm notification发送设备到设备 gcm 通知
【发布时间】:2016-05-19 21:31:18
【问题描述】:

谁能指出一个链接,我可以很容易地了解如何使用 GCM 进行设备到设备通知。将设备作为服务器和接收器需要什么。

让我解释一下我的要求,你们可能对我的问题有更好的解决方案

我想通过单击按钮向一个或一组人发送通知(预定义消息),其他用户也可以这样做。

谢谢

【问题讨论】:

  • 你需要使用gcm中的设备组developers.google.com/cloud-messaging/…
  • 发送消息应该/只能在您的服务器上完成,因为您必须公开您的 api 密钥才能发送它们。只需向您的服务器发送一条消息,表明您想要推送,然后让服务器完成工作

标签: android notifications google-cloud-messaging


【解决方案1】:

让我试着解释一下基本的工作原理 你需要-一台服务器和两部手机

第 1 步您有一个服务器,两部手机都连接到该服务器。

第 2 步您可以通过 link 访问 Google Developers 页面

第 3 步您在那里创建一个新项目,然后将 GCM API 添加到您的项目中。保存项目的 sender_id。还要检查 API 的凭据并添加新的 浏览器密钥 并保存该密钥。

第 4 步 将 GCM 添加到您的项目中。为此,请转到安装 Eclipse 的根目录。然后导航到文件夹extras/google/google-play-services/libsproject/,然后将那里的文件夹复制到另一个位置。然后将该文件夹导入 Eclipse 中的工作区并更改其属性以使其成为库。然后将其添加到您的应用程序项目中。

第 5 步您需要在 GCM 中注册您的手机。为此,您可以使用这样的示例代码

// to start process
public void registerDevice() {
    // TODO Auto-generated method stub
    try {
        if (checkPlayServices()) {
             regid = getRegistrationId();

            if (regid.isEmpty()) { // creating a new reg id
                registerInBackground();
            } else
                saveid();
        } else{

        }
    } catch (Exception e) {
    }
}

// to create a new id
private void registerInBackground() {
    // TODO Auto-generated method stub
    try {

        AsyncRegister logMeIn = new AsyncRegister("<you project id here>", context);

        logMeIn.doneWith = this;
        logMeIn.execute();

        System.out.println("execute complete");

    } catch (Exception e) {
    }
}

// to fetch stored registration id
private String getRegistrationId() {
    // TODO Auto-generated method stub
    try {


        int i=myPrefs.getInt("cuid", 0);

        if(id!=i){
            myPrefs.edit().putInt("cuid", id).commit();

            int registeredVersion = myPrefs.getInt("appversion",
                    Integer.MIN_VALUE); // to check if app is updated
            int currentVersion = getAppVersion();

            if (registeredVersion != currentVersion) {
                myPrefs.edit().putInt("appversion",currentVersion).commit();
            }

            return "";
        }

        String registrationId = myPrefs.getString("regid", ""); 

        if (registrationId.isEmpty()) {
            return "";
        }

        int registeredVersion = myPrefs.getInt("appversion",
                Integer.MIN_VALUE); // to check if app is updated
        int currentVersion = getAppVersion();

        if (registeredVersion != currentVersion) {
            myPrefs.edit().putInt("appversion", currentVersion).commit();
            return "";
        }

        // when id is stored previously then this is called
        return registrationId;
    } catch (Exception e) {
    }
}

//to fetch the current app version
private int getAppVersion() throws NameNotFoundException {
    // TODO Auto-generated method stub

        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(
                context.getPackageName(), 0);
        return packageInfo.versionCode;
}

// to check if play services are there on the device
private boolean checkPlayServices() {
    // TODO Auto-generated method stub
    try {

        int resultCode = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(context);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                toSend = new Bundle();
                  //tell user google play needs to be updated

            } else {
                //tell user their system does not support GCM
            }

            myPrefs.edit().putBoolean("support", false).commit();

            return false;
        }
        myPrefs.edit().putBoolean("support", true).commit();
        return true;
    } catch (Exception e) {
    }
}

第 6 步 保存注册 ID 并将其传递到您的服务器进行保存。

第 7 步 当用户单击发送按钮时,将消息传递到您的服务器。服务器将提取与 DB 一起存储的 id 列表,然后将消息与这些 id 一起推送到 GCM。

第 8 步 现在 GCM 会将该消息发送到所有为其提供 id 的手机。

第 9 步在您的应用中添加一个接收器,用于接收来自 GCM 的文本,然后将其显示给用户。

这是一个简短的解释。如果您想详细了解这一点,请阅读在线教程。否则,您也可以在我的群组中给我发短信(您可以在我的个人资料描述中找到链接),我很乐意为您提供帮助。

【讨论】:

    【解决方案2】:
    String postData = "{ \"registration_ids\": [ \"" + OtherDeviceToken+ "\" ], " +
                              "\"delay_while_idle\": true, " +
                              "\"data\": {\"tickerText\":\"Your title\", "+
                              "\"contentTitle\":\"AppName\", " +
                         "\"message\": \" " + edtYourMessage.getText().toString() + "\"}}";
    
    MediaType JSON= MediaType.parse("application/json; charset=utf-8");
     RequestBody body = RequestBody.create(JSON, postData);
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
    .url("https://android.googleapis.com/gcm/send")
    .addHeader("Content-Type", "application/json")
    .addHeader("Authorization", "key=" + SERVER_API_KEY)
    .post(body)
    .build();
    

    执行okkhttp请求。

    【讨论】:

      猜你喜欢
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      相关资源
      最近更新 更多