1

我正在开发一个应用程序,我必须向 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);
        }
}
4

3 回答 3

1

如果它还没有解决..我想这可能是问题的原因......添加这个

 <uses-permission android:name="my_app_package.permission.C2D_MESSAGE" />

到您的 android 清单 xml。

希望能帮助到你。

于 2012-12-26T07:04:20.883 回答
1

您应该包含您的日志输出,以便我们知道您运行应用程序时会发生什么。您还应该查看 GCM 演示并将其用作您的基础,因为您似乎缺少一些重要的代码:http ://code.google.com/p/gcm/source/browse/samples/gcm-demo-客户端/src/com/google/android/gcm/demo/app/DemoActivity.java

如果您从 GCM 获得注册 ID,则您已在 GCM 注册(您是否获得了注册 ID?),下一步是在您的服务器上注册。这应该发生在以下 If 块的 else 中:

if (regId.equals(""))

并且在您的 GCM 服务中的 onRegistered 中也是如此。这是当您的应用成功注册到 GCM 时将触发的回调。尝试注册后立即获取注册 ID 不会输出任何内容,因为与 GCM 的注册是异步的。

于 2012-12-24T16:51:14.913 回答
0

您必须输入您的unique_ID(即 12 个字符)CommonUtilities.java

/**
* Google API project id registered to use GCM.
*/
static final String SENDER_ID = 123456789012; <----
于 2012-12-24T10:45:54.287 回答