我是安卓新手。我正在尝试将消息(GCM)从我的服务器(Java)发送到 Android 手机。在调试服务器代码时,我发现消息已成功发送到安卓手机。但是我无法在电话中收到任何消息。我试图在 onMessage() 中设置一个断点,但没有用。
在收到来自服务器的每条消息后,我的手机只会收到一个警报框,例如:“项目没有响应。你想关闭它吗? ”它在警报框中有 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();
}
}
谁能帮我解决这个问题。即使我的服务器成功发送消息,为什么我没有在电话中收到任何消息。