微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android c2dm自动注销

如何解决Android c2dm自动注销

当我向设备发送通知时,该设备会收到推送消息,但会收到\“ com.google.android.c2dm.intent.REGISTRATION \”意图,而不是\“ com.google.android.c2dm.intent 。接收\”。 如果我尝试发送第二条通知,则会收到Google的“ NotRegistered \”错误。 设备注册正常(我想),因为我得到了身份验证令牌“ APA91 ... -119个字符-”。 这是我开始注册设备的代码
Intent registrationIntent = new Intent(\"com.google.android.c2dm.intent.REGISTER\");
registrationIntent.putExtra(\"app\",PendingIntent.getbroadcast(this,new Intent(),0));
registrationIntent.putExtra(\"sender\",\"EMAIL\");
startService(registrationIntent);
发件人(这在Android设备中完成):
HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(\"https://www.google.com/accounts/ClientLogin\");

    try {

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair(\"Email\",\"SAME AS LOGIN\"));
        nameValuePairs.add(new BasicNameValuePair(\"Passwd\",\"****\"));
        nameValuePairs.add(new BasicNameValuePair(\"accountType\",\"HOSTED_OR_GOOGLE\"));
        nameValuePairs.add(new BasicNameValuePair(\"source\",\"Google-cURL-Example\"));
        nameValuePairs.add(new BasicNameValuePair(\"service\",\"ac2dm\"));

        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        String line = \"\";
        while ((line = rd.readLine()) != null) {
            Log.e(\"C2DM\",line);
            if (line.startsWith(\"Auth=\")) {
                s = line.substring(5);//LOGIN TOKEN
                Log.i(\"C2DM\",\"Token get: \"+ s);
                    String auth_key = s; /* GETTING THE AUTH TOKEN FROM SERVER */
                    StringBuilder postDataBuilder = new StringBuilder();
                    postDataBuilder.append(ParaM_REGISTRATION_ID).append(\"=\").append(auth_key);
                    postDataBuilder.append(\"&\").append(ParaM_COLLAPSE_KEY).append(\"=\").append(\"0\");
                    postDataBuilder.append(\"&\").append(\"data.info\").append(\"=\").append(URLEncoder.encode(\"Invitar\",UTF8));
            URLEncoder.encode(telephonyManager.getdeviceid(),UTF8));

            byte[] postData = postDataBuilder.toString().getBytes(UTF8);

            URL url = new URL(\"https://android.clients.google.com/c2dm/send\");

            HttpURLConnection conn = (HttpURLConnection) 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_key);

            OutputStream out = conn.getoutputStream();
            out.write(postData);
            out.close();

            int responseCode = conn.getResponseCode();

            Log.e(\"C2DM\",String.valueOf(responseCode));
            // Validate the response code

            if (responseCode == 401 || responseCode == 403) {
                Log.e(\"C2DM\",\"Unauthorized - need token\");
            }

            // Check for updated token header
            String updatedAuthToken = conn.getHeaderField(UPDATE_CLIENT_AUTH);
            if (updatedAuthToken != null && !auth_key.equals(updatedAuthToken)) {
                Log.i(\"C2DM\",\"Got updated auth token from datamessaging servers: \"+ updatedAuthToken);
                Editor edit = prefManager.edit();
                edit.putString(AUTH,updatedAuthToken);
            }

            String responseLine = new BufferedReader(new InputStreamReader(conn.getInputStream())).readLine();

            if (responseLine == null || responseLine.equals(\"\")) {
                Log.i(\"C2DM\",\"Got \" + responseCode+ \" response from Google AC2DM endpoint.\");
                throw new IOException(\"Got empty response from Google AC2DM endpoint.\");
            }

            String[] responseParts = responseLine.split(\"=\",2);
            if (responseParts.length != 2) {
                Log.e(\"C2DM\",\"Invalid message from google: \" + responseCode+ \" \" + responseLine);
                throw new IOException(\"Invalid response from Google \"+ responseCode + \" \" + responseLine);
            }

            if (responseParts[0].equals(\"id\")) {
                Log.i(\"C2DM\",\"Successfully sent data message to device: \"+ responseLine);
                retval = 1;
            }

            if (responseParts[0].equals(\"Error\")) {
                String err = responseParts[1];
                Log.w(\"C2DM\",\"Got error response from Google datamessaging endpoint: \"+ err);
                // No retry.
                throw new IOException(err);
            }
                return sendMessage();
            }

        }
    } catch (IOException e) {
        e.printstacktrace();
    }
我的收件人:
public void onReceive(Context context,Intent intent) {
    Log.d(\"C2DM\",\"Intent recieved: \"+intent.getAction());
    if (intent.getAction().equals(\"com.google.android.c2dm.intent.REGISTRATION\")) {
        handleRegistration(context,intent);
    } else if (intent.getAction().equals(\"com.google.android.c2dm.intent.RECEIVE\")) {
        handleMessage(context,intent);
    }
 }

private void handleRegistration(Context context,Intent intent) {
    String registration = intent.getStringExtra(\"registration_id\");
    if (intent.getStringExtra(\"error\") != null) {
        // Registration Failed,should try again later.
        Log.d(\"C2DM\",\"registration Failed\");
        String error = intent.getStringExtra(\"error\");
        if(error == \"SERVICE_NOT_AVAILABLE\"){
            Log.d(\"c2dm\",\"SERVICE_NOT_AVAILABLE\");
        }else if(error == \"ACCOUNT_MISSING\"){
            Log.d(\"c2dm\",\"ACCOUNT_MISSING\");
        }else if(error == \"AUTHENTICATION_Failed\"){
            Log.d(\"c2dm\",\"AUTHENTICATION_Failed\");
        }else if(error == \"TOO_MANY_REGISTRATIONS\"){
            Log.d(\"c2dm\",\"TOO_MANY_REGISTRATIONS\");
        }else if(error == \"INVALID_SENDER\"){
            Log.d(\"c2dm\",\"INVALID_SENDER\");
        }else if(error == \"PHONE_REGISTRATION_ERROR\"){
            Log.d(\"c2dm\",\"PHONE_REGISTRATION_ERROR\");
        }
    } else if (intent.getStringExtra(\"unregistered\") != null) {
        //HERE IS WHERE ARRIVES THE SEND NOTIFICATION (NOT THE UNREGISTER NOTIFICATION)
        Log.d(\"C2DM\",\"unregistered: \"+intent.getStringExtra(\"unregistered\"));

    } else if (registration != null) {
        Log.d(\"C2DM\",registration);
        /* SENDING THE AUTH TOKET TO SERVER */
    }
}
我在设备上执行推送通知,因为如果在服务器端执行操作,则总是会收到一个“ InvalidRegistration \”错误。 奇怪的是,好像我已经注册了,但是当我发送一条推送消息时,就像Google取消注册了该设备。 有任何想法/建议吗?     

解决方法

我有同样的问题,并已解决。 这是错误的代码
<receiver android:name=\".MyC2DMReceiver\"
            android:permission=\"com.google.android.c2dm.permission.SEND\">
      <intent-filter>
          <action android:name=\"com.google.android.c2dm.intent.REGISTRATION\" />
          <category android:name=\"com.package.myapp\" />
      </intent-filter>
      <intent-filter>
          <action android:name=\"com.google.android.c2dm.permission.RECEIVE\" />
          <category android:name=\"com.package.myapp\" />
      </intent-filter>
  </receiver>
我有写意图而不是许可
com.google.android.c2dm.permission.RECEIVE => com.google.android.c2dm.intent.RECEIVE
然后它可以正常工作 因此RECEIVE的包裹名称错误 正确的代码
<receiver android:name=\".MyC2DMReceiver\"
            android:permission=\"com.google.android.c2dm.permission.SEND\">
      <intent-filter>
          <action android:name=\"com.google.android.c2dm.intent.REGISTRATION\" />
          <category android:name=\"com.package.myapp\" />
      </intent-filter>
      <intent-filter>
          <action android:name=\"com.google.android.c2dm.intent.RECEIVE\" />
          <category android:name=\"com.package.myapp\" />
      </intent-filter>
  </receiver>
    ,有时候,解决方案就摆在您的面前,而您却看不到,对于那些遇到与我相同的问题,问题出在清单上,我的软件包名称写得不好     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。