Android GCMIntentService onMessage问题

时间:2012-11-21 11:49:01

标签: android google-cloud-messaging

我是Android新手。我试图从我的服务器(Java)发送消息(GCM)到Android手机。在调试服务器代码时,我发现消息已成功发送到Android手机。但是我无法通过电话收到任何消息。我试图在onMessage()中有一个断点,但没有用。

在服务器发出的每条消息之后,我的手机只收到一个警告框:“ PROJECT没有响应。你要关闭吗?”警报中有2个按钮方框:等等,好的。

任何人都可以帮助我。即使我的服务器成功发送了消息,为什么我没有收到电话中的任何消息。我的客户端和服务器代码如下:

服务器

public void sendPushNotification(String theRegistrationId, String thePushMsg)
    {
        //Used to transmit messages to the Google Cloud Messaging service.
        Sender aGcmSender = new Sender(API_KEY); 

        //Constructing message which need to be transmitted to android device.      
        Message aMessage = new Message.Builder().addData("message", thePushMsg).build(); 
        try 
        {
            Result result = aGcmSender.send(aMessage, theRegistrationId, 1); 
        }
        catch (Exception theException) 
        {

        }
    }

客户端: 的AndroidManifest.xml:

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

<permission android:name="com.test.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.test.app.permission.C2D_MESSAGE" />

<uses-permission android:name="android.permission.INTERNET" />

<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_OWNER_DATA" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />

<uses-permission android:name="android.permission.SET_DEBUG_APP"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:name="MyApplication">
    <uses-library android:name="com.google.android.maps" />

    <activity
        android:name=".view.welcome.WelcomeView"
        android:screenOrientation="portrait" >
        <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.test.app" />
      </intent-filter>
    </receiver>

    <service android:name="com.test.app.GCMIntentService" />

</application>

GCMIntentService:

package com.test.app;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService
{
    private static final String TAG = "===GCMIntentService===";
    private static String GCM_SENDER_ID = "697532398835"; //Project id in Google API Console

    public GCMIntentService() 
    {
        super(GCM_SENDER_ID);
        Log.d(TAG, "GCMIntentService init");
    }

    @Override
    protected void onRegistered(Context theContext, String theRegistrationId) 
    {
        Log.i(TAG, "Device registered: regId = " + theRegistrationId);
    }

    @Override
    protected void onUnregistered(Context theContext, String theArg) 
    {
        Log.i(TAG, "unregistered = "+theArg);
    }

    @Override
    protected void onMessage(Context theContext, Intent theIntent) 
    {
        Log.i(TAG, "new message= ");

        String aMessage = theIntent.getStringExtra("message");
        sendGCMIntent(theContext, aMessage);
    }

    private void sendGCMIntent(Context theContext, String theMessage) 
    {
        Intent aBroadcastIntent = new Intent();
        aBroadcastIntent.setAction("GCM_RECEIVED_ACTION");
        aBroadcastIntent.putExtra("gcm", theMessage);
        theContext.sendBroadcast(aBroadcastIntent);
    }

    @Override
    protected void onError(Context theContext, String theErrorId) 
    {
        Log.i(TAG, "Received error: " + theErrorId);
    }

    @Override
    protected boolean onRecoverableError(Context theContext, String theErrorId) 
    {
        return super.onRecoverableError(theContext, theErrorId);
    }
}

我的活动:

package com.test.app.view.welcome;

import java.net.MalformedURLException;
import java.net.URL;

import com.google.android.gcm.GCMRegistrar;

public class LoginView extends Activity implements Runnable {

    Typeface itsTypeFace;
    IntentFilter gcmFilter;
    String broadcastMessage = "No broadcast message";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login);

        gcmFilter = new IntentFilter();
        gcmFilter.addAction("GCM_RECEIVED_ACTION");         

    }

    private BroadcastReceiver gcmReceiver = new BroadcastReceiver() 
    {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            broadcastMessage = intent.getExtras().getString("gcm");
            if (broadcastMessage != null) 
            {
                System.out.println(broadcastMessage);
            }
        }
    };

    /**
     * Method to avoid back key press for a higher api level 2.0 and above
     */
    @Override
    public void onBackPressed() {}

    /** If the user changes the orientation of his phone, the current activity
        is destroyed, and then re-created.  This means that our broadcast message
        will get wiped out during re-orientation.
        So, we save the broad cast message during an onSaveInstanceState()
        event, which is called prior to the destruction of the activity.
    */
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) 
    {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putString("BroadcastMessage", broadcastMessage);

    }

    /** When an activity is re-created, the os generates an onRestoreInstanceState()
        event, passing it a bundle that contains any values that you may have put
        in during onSaveInstanceState()
        We can use this mechanism to re-display our last broadcast message.
    */
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) 
    {
        super.onRestoreInstanceState(savedInstanceState);
        broadcastMessage = savedInstanceState.getString("BroadcastMessage");
    }

    /** If our activity is paused, it is important to UN-register any
        broadcast receivers.
    */
    @Override
    protected void onPause() 
    {
        unregisterReceiver(gcmReceiver);
        super.onPause();
    }

    /** When an activity is resumed, be sure to register any
        broadcast receivers with the appropriate intent
    */
    @Override
    protected void onResume() 
    {
        super.onResume();
        registerReceiver(gcmReceiver, gcmFilter);
    }

    /** 
     * The call to GCMRegistrar.onDestroy()
     */
    @Override
    public void onDestroy() 
    {
        GCMRegistrar.onDestroy(this);
        super.onDestroy();
    }
}

任何人都可以帮我解决这个问题。即使我的服务器成功发送了消息,为什么我没有收到电话中的任何消息。

1 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的方法。在部署我的应用程序(服务器端)时,我发现json-simple.jar存在一些问题。

我已将jar升级为 json-simple-1.1.1.jar 并且它有效!

而且正如NickT建议的那样,我更新了我的规范如下:

Sender aGcmSender = new Sender(API_KEY);     
//Constructing message which need to be transmitted to android device.      
Message aMessage = new Message.Builder().addData("message", thePushMsg).build(); 

Result result = aGcmSender.send(aMessage, REG_ID, 1); //Transmitting message to android device
if(result != null)
{
    if(result.getMessageId() != null)
    {
        String aCanonicalRegistrationId = result.getCanonicalRegistrationId();
        if(aCanonicalRegistrationId != null)
        {
            //Updated my database with aCanonicalRegistrationId (replaced REG_ID)
        }
    }
    else 
    {  
        String error = result.getErrorCodeName();
        if (error.equals(Constants.ERROR_NOT_REGISTERED)) {    
            // application has been removed from device - unregister database
        }
    }
   }
}

谢谢。