我正在开发一个应用程序,我必须向 GCM 注册我的设备,但它没有注册我的设备。我已授予清单中的应用程序的所有权限我的日志在这里
注册onGCM的方法是
public void register()
{
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
if (GCMRegistrar.isRegistered(this))
{
Log.d("info", GCMRegistrar.getRegistrationId(this));
}
final String regId = GCMRegistrar.getRegistrationId(this);
System.out.println("registration ID is " + regId);
if (regId.equals(""))
{
System.out.println("registration ID is " + regId);
GCMRegistrar.register(this, CommonUtilities.SENDER_ID);
Log.d("info", GCMRegistrar.getRegistrationId(this));
System.out.println("Get Reg ID: " + GCMRegistrar.getRegistrationId(this));
}
else
{
Log.d("info", "already registered as" + regId);
}
}
我的 GCM 服务是
public class GCMService extends GCMBaseIntentService
{
public GCMService()
{
super(CommonUtilities.SENDER_ID);
}
public void generateNotification(Context context, String message)
{
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, OffersActivity.class);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS;
notificationManager.notify(0, notification);
}
@Override
protected void onError(Context arg0, String arg1)
{
}
@Override
protected void onMessage(Context arg0, Intent arg1)
{
Log.d("GCM", "RECIEVED A MESSAGE");
generateNotification(arg0, "New Offers Are Available");
}
@Override
protected void onRegistered(Context arg0, String arg1)
{
}
@Override
protected void onUnregistered(Context arg0, String arg1)
{
}
}
我的清单是
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- Map Location permissions -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Notification permissions -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_OWNER_DATA" />
<permission
android:name="myPackage.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<!-- Storage permissions -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Internet permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Call Phone permissions -->
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="cmyPackage.SplashScreen"
android:label="AppName"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="myPackage.CatagoriesActivity"
android:configChanges="keyboardHidden|orientation|keyboard"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name="myPackage.OffersActivity"
android:configChanges="keyboardHidden|orientation|keyboard"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name="myPackage.SettingsActivity"
android:configChanges="keyboardHidden|orientation|keyboard"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name="myPackage.OfferDetailsAtivity"
android:configChanges="keyboardHidden|orientation|keyboard"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name="myPackage.DealInfoActivity"
android:configChanges="keyboardHidden|orientation|keyboard"
android:theme="@android:style/Theme.NoTitleBar" >
</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="myPackage" />
</intent-filter>
</receiver>
<service android:name=".GCMService" />
<uses-library android:name="com.google.android.maps" />
</application>
I/System.out(17369): Registering
D/GCMRegistrar(17369): resetting backoff for packageName
V/GCMRegistrar(17369): Registering app packageName of sendersID xxxxxxxxxxxxxx
registration id Still Empty
(17369): Not yet registered
我的应用程序中有多个包
public final class CommonUtilities
{
public static final String SENDER_ID = "xxxxxxxxxxxxx";
static final String TAG = "AndroidHive GCM";
public static final String DISPLAY_MESSAGE_ACTION = "myPackage.pushNotifications.DISPLAY_MESSAGE";
static final String EXTRA_MESSAGE = "message";
static void displayMessage(Context context, String message)
{
Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
intent.putExtra(EXTRA_MESSAGE, message);
context.sendBroadcast(intent);
}
}