如何为GCM创建库,库创建不起作用

时间:2017-08-31 11:32:53

标签: java android

我正在尝试为我的应用创建一个库,在我的所有应用中都有推送通知。

我想拿这个包并创建一个库

在GCM中,我有任何限制吗?因为它看起来像获取包名称来生成ID_TOKEN

我有一个APP,其中包含我用于PUSH通知的类,它运行正常!

现在我已经迁移了这个包并创建了一个库,因为所有其他应用程序只是指向lib并且它将正常工作。

由于某些原因,他没有给lib打电话,我已经做了所有事情而且我做不到。

在GCM中注册ID并启动服务的代码如下:

        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);

上面的代码在我的MainActivity中 我认为通过这样做他已经调用了库

修改

  

我正在使用Eclipse和GCM

My class `RegistrationIntentService` 

    public class RegistrationIntentService extends IntentService {

    private static final String TAG = "RegServicePush";
    String newRegID = "";
    String GetEmail = "";

    public RegistrationIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        try {

            InstanceID instanceID = InstanceID.getInstance(this);
            String token = instanceID.getToken(Constants.GCM_SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);


            // TODO: Implement this method to send any registration to your
            // app's servers.
            sendRegistrationToServer(token, email);

        } catch (Exception e) {
            Log.d(TAG, "Failed to complete token refresh", e);
        }
        // Notify UI that registration has completed, so the progress indicator
        // can be hidden.

    }

    private void sendRegistrationToServer(String token, String email) {
        //MainActivity.newRegID = token;

        WebServerRegistrationTask webServer = new WebServerRegistrationTask();
        webServer.execute();
    }


    public class WebServerRegistrationTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            SharedPreferences sharedPreferences = PreferenceManager
                    .getDefaultSharedPreferences(RegistrationIntentService.this);

            URL url = null;
            try {
                url = new URL(Constants.WEB_SERVER_URL);
            } catch (MalformedURLException e) {
                e.printStackTrace();

                sharedPreferences.edit().putString(Constants.PREF_GCM_REG_ID, "").apply();
            }
            Map<String, String> dataMap = new HashMap<String, String>();
            dataMap.put("regID", newRegID);
            dataMap.put("appID", Constants.APP_ID);


            StringBuilder postBody = new StringBuilder();
            Iterator<Map.Entry<String, String>> iterator = dataMap.entrySet().iterator();

            while (iterator.hasNext()) {
                Entry<String, String> param = (Entry<String, String>) iterator.next();
                postBody.append(param.getKey()).append('=').append(param.getValue());
                if (iterator.hasNext()) {
                    postBody.append('&');
                }
            }
            String body = postBody.toString();
            byte[] bytes = body.getBytes();

            HttpURLConnection conn = null;
            try {
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                conn.setFixedLengthStreamingMode(bytes.length);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

                OutputStream out = conn.getOutputStream();
                out.write(bytes);
                out.close();
                String response = "";
                InputStream is = null;
                try {
                    is = conn.getInputStream();
                    int ch;
                    StringBuffer sb = new StringBuffer();
                    while ((ch = is.read()) != -1) {
                        sb.append((char) ch);
                    }
                    response = sb.toString();

                } catch (IOException e) {
                    throw e;
                } finally {
                    if (is != null) {
                        is.close();
                    }
                }
                int status = conn.getResponseCode();
                if (status == 200) {
                    if (response.equals("1")) {
                        sharedPreferences.edit().putString(Constants.PREF_GCM_REG_ID, newRegID).apply();
                        Intent registrationComplete = new Intent(Constants.SERVER_SUCCESS);
                        LocalBroadcastManager.getInstance(RegistrationIntentService.this)
                                .sendBroadcast(registrationComplete);
                    }
                } else {
                    throw new IOException("Request failed with error code " + status);
                }
            } catch (ProtocolException pe) {
                pe.printStackTrace();
                sharedPreferences.edit().putString(Constants.PREF_GCM_REG_ID, "").apply();

            } catch (IOException io) {
                io.printStackTrace();
                sharedPreferences.edit().putString(Constants.PREF_GCM_REG_ID, "").apply();

            } finally {
                if (conn != null) {
                    conn.disconnect();
                }
            }

            return null;
        }
    }
}

0 个答案:

没有答案