我正在開發一個應用程序,我必須使用GCM註冊我的設備,但它沒有註冊我的設備。我已授予的所有權限在menifest 我日誌中的應用程序是在這裏Android的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)
{
}
}
和我menifest是
<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);
}
}
您的模擬器(或真實手機)是Google API的嗎?你有沒有檢查http://stackoverflow.com/questions/11341305/android-gcm-gcmregistrar-gives-empty-registration-id? – sabadow
我正在測試真實設備2.2 Froyo 7英寸平板電腦三星 – Farrukh
@ Farrukh:你解決了這個問題,我面臨同樣的問題。請幫助。 –