首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发送设备到设备gcm通知

发送设备到设备gcm通知
EN

Stack Overflow用户
提问于 2016-05-19 20:55:12
回答 2查看 98关注 0票数 0

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

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

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

谢谢

EN

回答 2

Stack Overflow用户

发布于 2016-05-19 21:23:58

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

步骤1您有一台服务器,两部电话都连接到该服务器。

Step 2您可以通过以下link转到谷歌开发人员页面

Step 3您在其中创建一个新项目,然后将GCM添加到您的项目中。保存项目的sender_id。另外,检查API的凭据,添加一个新的浏览器密钥并保存该密钥。

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

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

代码语言:javascript
复制
// 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并将其传递到服务器进行保存。

Step 7当用户单击send按钮时,会向您的服务器传递一条消息。服务器将取出存储在数据库中的ids列表,然后将消息连同这些ids一起推送到GCM。

Step 8 Now GCM会将该消息发送到ids为其提供的所有电话。

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

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

票数 0
EN

Stack Overflow用户

发布于 2016-05-19 21:31:19

代码语言:javascript
复制
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请求。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37323850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档