我搜索了互联网,但无法让我的 GCM 服务器/客户端正常工作。
我怀疑这是客户端的问题,因为我从服务器端获得了成功响应。
无论如何,我正在剪辑客户端代码和服务器代码。
顺便说一句,我可以注册我的设备 ID。
服务器端
Sender sender = new Sender(apiKey);
Message message = new Message.Builder()
.delayWhileIdle(true)
.addData("AUB",text)
.build();
Result result = null;
try {
result = sender.send(message, deviceID, 5);
return "sent " + "Message ID:" + result.getMessageId() + message.getData();
} catch (IOException e) {
e.printStackTrace();
return "error: " + result.getErrorCodeName();
}
客户端
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
registerReceiver(mHandleMessageReceiver, new IntentFilter(
Constants.DISPLAY_MESSAGE_ACTION));
String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, Constants.SENDER_ID);
Log.v(TAG, "Registered");
} else {
Log.v(TAG, "Already registered");
Log.v(TAG, GCMRegistrar.getRegistrationId(this));
}
}
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(
Constants.EXTRA_MESSAGE);
Log.v(TAG, newMessage);
}
};
GCMIntentService
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
super(Constants.SENDER_ID);
}
@Override
protected void onError(Context arg0, String arg1) {
}
@Override
protected void onRegistered(Context context, String registrationId) {
Log.v(TAG, "Device registered: regId = " + registrationId);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = "AUB";
Constants.displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity_.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
}
显现
<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="PACKAGE" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" android:enabled="true"/>
<permission
android:name="PACKAGE.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<!-- Permissions for GCM -->
<!-- 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" />
<!-- Keeps the processor from sleeping when a message is received. -->
<!-- -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="PACKAGE.permission.C2D_MESSAGE" />
<uses-permission android:name="PACKAGE.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
我已经解决了这个问题。显然我们的网络阻塞了 5228 端口。