这是我用来注册我的应用程序的代码:
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
if (!GCMRegistrar.getRegistrationId(getApplicationContext()).equals("")) {
GCMRegistrar.register(getApplicationContext(), SENDER_ID);
}
这是我的清单权限
<uses-permission android:name="android.permission.INTERNET" />
<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" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.cidaut.blueparking.permission.MAPS_RECIEVE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
这是接收器
<service android:name=".GCMIntentService" />
<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.cidaut.blueparking" />
</intent-filter>
</receiver>
这是我的com.cidaut.blueparking.GCMIntentService.java
课
package com.cidaut.blueparking;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
}
@Override
protected void onError(final Context arg0, final String arg1) {
System.out.println("ERROR'D!!");
System.out.println(arg1);
}
@Override
protected void onMessage(final Context arg0, final Intent arg1) {
System.out.println("YES HELLO I AM DEBUGGER");
String message = "Tu aparcamiento ha exiprado.";
if (arg1.getExtras() != null) {
final Bundle extras = arg1.getExtras();
if (extras.containsKey("message")) {
message = extras.getString("message");
}
}
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setContentTitle("Blue Parking").setContentText(message)
.setSmallIcon(R.drawable.ic_launcher);
final Intent resultIntent = new Intent(this, MapScreen_.class);
final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(resultIntent);
final PendingIntent resultPendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
try {
final Uri notification = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final Ringtone r = RingtoneManager.getRingtone(
getApplicationContext(), notification);
r.play();
} catch (final Exception e) {
}
notificationManager.notify(1, mBuilder.build());
}
@Override
protected void onRegistered(final Context arg0, final String arg1) {
System.out.println("TOKEN: " + arg1);
}
@Override
protected void onUnregistered(final Context arg0, final String arg1) {
System.out.println("UNREGISTERED");
System.out.println(arg1);
}
}
所以,当我打电话GCMRegistrar.register
时它永远不会打电话onRegistered
......除非我在我的 Nexus 4 上尝试它。代码在我的 Nexus 4 上完美运行。我已经在三星 Galaxy S、三星 Galaxy Ace、三星 Galaxy SII、三星上测试过它Galaxy SIII、HTC Wildfire S、HTC Desire、HTC One X 和 ASUS Transformer,它根本无法工作。如果它在我的 Nexus 4 上不起作用,我的困惑就会少很多。我在这里做错了什么?