将设备发送到设备gcm通知

时间:2016-05-19 12:55:12

标签: android notifications google-cloud-messaging

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

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

我想在点击按钮时向一个或一组人发送通知(预先定义的消息),另一个用户也可以这样做。

由于

2 个答案:

答案 0 :(得分:0)

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

第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文本的接收器,然后将其显示给用户。

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

答案 1 :(得分:0)

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请求。

相关问题