GCM:来自GCMIntentService的onMessage()被调用多次?

7
我正在实现GCM到我的应用程序中。我已经按照developer.android.com上的GCM教程的所有步骤进行了操作。
我成功地从GCM获取了注册ID,并将此ID传递给我的应用程序服务器。因此,注册步骤已成功执行。
现在,当我的应用程序服务器向我的设备发送PUSH消息时,服务器会获得消息,例如SUCCESS=8 FAILURE=0等,即服务器成功地发送消息,但是OnMessage的调用次数取决于成功值而被调用8次。
我的设备通过组织wifi连接,没有任何代理设置。
我的问题是:为什么我接收到来自GCM的PUSH消息的次数与Success值一样多?Google为什么会传递那么多消息?原因可能是什么?
我的GCMBaseIntentService如下所示:
public class GCMIntentService extends GCMBaseIntentService {




    public GCMIntentService() {
        super(GCMActivity.SENDER_ID);
    }

    @Override
    protected void onError(Context arg0, String arg1) {

        Log.d("GCM", "RECIEVED A ERROR MESSAGE");
        // TODO Auto-generated method stub

    }

    @Override
    protected void onRegistered(Context arg0, String registrationId) {

//Send the registration id to my app server to store the reg id list
        GCMActivity.sendRegIdtoApplicationServer(registrationId);

    }


    @Override
    protected void onUnregistered(Context arg0, String registrationId) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.d("GCM", "RECIEVED A MESSAGE");

        // Get the data from intent and send to notificaion bar


        String data = intent.getStringExtra("data");
         String action = intent.getAction();

        if((action.equals("com.google.android.c2dm.intent.RECEIVE"))) {
            try {
                JSONObject jsonObject = new JSONObject(data);
                boolean updateStatus = jsonObject.getBoolean("update");



                if (updateStatus) {
                    //Showing Notification to user


                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }



    }

我的活动看起来像

public class GCMActivity extends Activity {

    public final static String SENDER_ID =  "872355133485";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gcm);

        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
          GCMRegistrar.register(this, SENDER_ID);

        } else {

//          sendRegIdtoApplicationServer(regId);
          Log.v("gh", "Already registered");
          System.out.println("RegisterID:"+ regId);
            }
        }



And Manifest file will be 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.aspire.gcmsample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

  <permission android:name="com.aspire.gcmsample.permission.C2D_MESSAGE" android:protectionLevel="signature" />

    <uses-permission android:name="com.aspire.gcmsample.permission.C2D_MESSAGE" />
    <!-- App receives GCM messages. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET" /> 
    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" android:debuggable="true">

        <activity
            android:name=".GCMActivity"
            android:label="@string/title_activity_gcm" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

       <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" 
                 android:permission="com.google.android.c2dm.permission.SEND" >
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                    <category android:name="com.aspire.gcmsample" />

                </intent-filter>
        </receiver>

       <service android:name=".GCMIntentService" />
    </application>


</manifest>

这是我的服务器端代码

        Properties apHeaders = new Properties();

        // Use your own credentials for the below three lines
        apHeaders.put("Content-Type", "application/json");
        apHeaders.put("Authorization","key="+apiKey);

        /*String data = "{\"registration_ids\":"+ devicesList +", \"data\":" +
                "{\"data\":{\"score\" : \"1-0\", \"scorer\" : \"Ronaldo\", \"time\" : \"44\"}}}";*/

        // get all registration Device ID from database
        ArrayList<String> deviceId = DBRegistration.getInstance().fetchRegId();

        String data = "{\"registration_ids\":"+ deviceId +", \"data\":" +
            "{\"data\":{\"update\" : \"true\", " +
            "\"file\": \"http://192.168.4.210:8080/FileDownload/DownloadFile?path=GCMSample.apk\"}}}";

        String result = DBRegistration.getInstance().sendRequest(url, data, apHeaders);**

        System.out.println("responseC"+ result);
        out.println(result);
        out.flush();

字符串 apServerUrl = "https://android.googleapis.com/gcm/send";

(该代码与Android GCM消息推送有关)
public String sendRequest(String apServerUrl, String payload, Properties headers) {

        // Setup Http URL connection

        HttpURLConnection connection = null;

        // Enable proxy if it needed.
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_URL, PROXY_PORT));

        try {
            URL url = new URL(apServerUrl);
            // Enable proxy if needed.
            connection = (HttpURLConnection) url.openConnection(proxy);
            //connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            Object[] keys = headers.keySet().toArray();
            for (int i = 0; i < keys.length; i++) {
                connection.setRequestProperty((String) keys[i],
                        (String) headers.get(keys[i]));
            }
            //connection.setRequestProperty("Content-Length", ""+ payload.length());
        } catch (Exception e) {
            System.out.println("Failed setting up HTTP Connection\n" + e);
        }

        // Send the Request
        String line = "";
        String returnedResponse = "";
        BufferedReader reader = null;

        System.out.println("Request: " + payload);

        try {
            OutputStream os = connection.getOutputStream();
            os.write(payload.toString().getBytes("UTF-8"));
            os.close();
            int status = connection.getResponseCode();            
            if (status != 200) {
                System.out.println("HTTP Error code " + status
                        + " received, transaction not submitted");
                reader = new BufferedReader(new InputStreamReader(connection
                        .getErrorStream()));
            } else {
                reader = new BufferedReader(new InputStreamReader(connection
                        .getInputStream()));
            }

            while ((line = reader.readLine()) != null) {
                returnedResponse += line;
            }
        } catch (Exception e) {
            returnedResponse = e.getMessage();
            System.out.println(e);
        } finally {
            try {
                if (reader != null)
                    reader.close();
                if (connection != null)
                    connection.disconnect();
            } catch (Exception e) {
                returnedResponse = e.getMessage();
                System.out.println(e);
            }
        }
        System.out.println(returnedResponse);
        return returnedResponse;
    }

来自GCM服务器对我的应用服务器的响应。

{"multicast_id":6552291927399218343,"success":13,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1356332586480671%79fa3a68f9fd7ecd"},{"message_id":"0:1356332586480674%79fa3a68f9fd7ecd"},{"message_id":"0:1356332586481759%79fa3a68f9fd7ecd"},{"message_id":"0:1356332586480807%79fa3a68f9fd7ecd"},{"message_id":"0:1356332586481944%79fa3a68f9fd7ecd"},{"message_id":"0:1356332586480996%79fa3a68f9fd7ecd"},{"message_id":"0:1356332586481939%79fa3a68f9fd7ecd"},{"message_id":"0:1356332586482000%79fa3a68f9fd7ecd"},{"message_id":"0:1356332586481997%79fa3a68f9fd7ecd"},{"message_id":"0:1356332586481942%79fa3a68f9fd7ecd"},{"message_id":"0:1356332586482003%79fa3a68f9fd7ecd"},{"message_id":"0:1356332586481948%79fa3a68f9fd7ecd"},{"message_id":"0:1356332586480995%79fa3a68f9fd7ecd"}]}

在我的 GCM 应用程序中它没有执行,如果您问我,这听起来更像是您代码中的一个错误 - 您可以展示一下代码吗? - ligi
1
发送具体注册ID的消息的服务器端代码在哪里? - appukrb
请添加服务器端代码。 - ligi
我添加了我的服务器端代码。 - Lakshmanan
2个回答

5
你从GCM发送给服务器的响应是什么样子的?有可能你已经使用不同的ID注册了相同的设备8次,这似乎是最可能的原因。
你还应该检查发送到GCM服务器的代码,以确保不会发送相同的注册ID八次。
响应格式在这里有文档:https://developer.android.com/google/gcm/gcm.html#response 基于此,我建议您查看是否收到任何规范化的ID,这将帮助您显示您已经多次注册了相同的设备。您可以在这里阅读有关如何处理规范化ID的更多信息:android GCM get original id of canonical id

@selsine;我已添加了GCM服务器的响应,请查看,Canonical id为0。 - Lakshmanan
感谢selsine:我找到了问题所在。我的服务器端代码有问题,就像你的假设一样,单个注册ID在我的有效载荷数据的设备列表中被添加了多次。 - Lakshmanan

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接