GCM注册在我的模拟器上无法工作

13

我在我的应用程序中使用了GCM服务,在进行更多的研究后,参考了Stack Overflow成员的代码,当我尝试在模拟器上测试代码时,我没有找到任何注册信息告诉我我的模拟器已注册。同时我也创建了Google API账户以接收任何报告,但是我没有找到任何信息。所以我需要你的帮助:

1- 请检查我的代码,并在出现错误时给我反馈一个完整的示例:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.elarabygroup"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <permission
        android:name="com.example.elarabygroup.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.elarabygroup.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />


    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ElarabyGroup"
            android:label="@string/title_activity_elaraby_group" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </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="com.example.elarabygroup" />
            </intent-filter>
        </receiver>

        <service android:name=".GCMIntentService" />
        <!--
            android:name=".GCMIntentService"
            android:enabled="true" />
        -->
    </application>

</manifest>

package com.example.elarabygroup;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import com.google.android.gcm.GCMRegistrar;


public class ElarabyGroup extends Activity {
    private String TAG;
    private String SENDER_ID = "222874571774";
    private WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_elaraby_group);

        try {

            Intent registrationIntent = new Intent(
                    "com.google.android.c2dm.intent.REGISTER");

            /* Registering for GCM /An Android application needs to register with GCM servers before it can receive messages*/
            registrationIntent.putExtra("app",
                    PendingIntent.getBroadcast(this, 0, new Intent(), 0));
            registrationIntent.putExtra("sender", SENDER_ID);
            startService(registrationIntent);

            Log.i(TAG, "[checkNotifRegistration] checkDevice");
            GCMRegistrar.checkDevice(this);
            Log.i(TAG, "[checkNotifRegistration] checkManifest");
            GCMRegistrar.checkManifest(this);
            if (GCMRegistrar.isRegistered(this)) {
                Log.i(TAG,
                        "[checkNotifRegistration] reg id : "
                                + GCMRegistrar.getRegistrationId(this));
            }
            final String regId = GCMRegistrar.getRegistrationId(this);
            if (regId.equals("")) {
                // SENDER_ID is my project id into google account url
                GCMRegistrar.register(this, SENDER_ID);
                Log.i(TAG,
                        "[checkNotifRegistration] reg id : "
                                + GCMRegistrar.getRegistrationId(this));

            } else {
                Log.i(TAG, "[checkNotifRegistration] already registered as : "
                        + regId);
            }
        } catch (Exception e) {
            Log.e(TAG, "[checkNotifRegistration] Exception : " + e.getMessage());
            e.printStackTrace();
        }

        /*
         * GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this);
         * 
         * 
         * final String regId = GCMRegistrar.getRegistrationId(this); if
         * (regId.equals("")) { GCMRegistrar.register(this, "1111111111"); }
         * else { Log.v(TAG, "Already registered"); }
         */
        try {

            ConnectivityManager con = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

            if (con.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
                    && con.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);

                builder.setMessage("No Internet connection");
                AlertDialog alert = builder.create();
                alert.show();

            } else

            {

                webView = (WebView) findViewById(R.id.webView1);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.loadUrl("http://m.elarabygroup.com");
            }

        } catch (Exception e) {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);

            builder.setMessage(e.getMessage().toString());

            AlertDialog alert = builder.create();

            String url = "http://m.elarabygroup.com/";

            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);

        }

    }
    /*
    public class MyBroadcastReceiver extends BroadcastReceiver {

        @Override
        public final void onReceive(Context context, Intent intent) {
            GCMIntenetService.runIntentInService(context, intent);
            setResult(Activity.RESULT_OK, null, null);
        }
    }
    */

}
/*
 * @Override public boolean onCreateOptionsMenu(Menu menu) {
 * getMenuInflater().inflate(R.menu.activity_elaraby_group, menu); return true;
 * } }
 */

package com.example.elarabygroup;

import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMRegistrar;

import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.provider.Settings.Secure;
import android.util.Log;

public class GCMIntenetService extends GCMBaseIntentService {
    private static  String GCM_SENDER_ID = "1111111111111";

    public GCMIntenetService() {
        super();
    }

    @Override
    protected void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        GCMRegistrar.setRegisteredOnServer(context, true);
    }

    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.i(TAG, "Device unregistered");
        if (GCMRegistrar.isRegisteredOnServer(context)) {
            String regId = "";
            Log.i(TAG, "unregistering device (regId = " + regId + ")");
            GCMRegistrar.setRegisteredOnServer(context, false);
        } else {
            // This callback results from the call to unregister made on
            // ServerUtilities when the registration to the server failed.
            Log.i(TAG, "Ignoring unregister callback");
        }
    }

    @Override
    protected void onError(Context context, String errorId) {
        // push error processing
    }

    @Override
    protected void onMessage(Context arg0, Intent arg1) {
        Log.i(TAG, "Received message");
        Log.i(TAG, "EXTRAS" + arg1.getExtras());
        // String message = getString(R.string.gcm_message);
        generateNotification(arg0,
                arg1.getStringExtra("Please download our new updates"));
        // notifies user about message

    }

    private void generateNotification(Context arg0, String stringExtra) {
        // TODO Auto-generated method stub

    }

    public static void registerInGCMService(Context context) {

        GCM_SENDER_ID = Secure.getString(context.getContentResolver(),
                Secure.ANDROID_ID);


        if (!checkIsGCMServiceAvailable(context)) {
            return;
        }
        final String regId = GCMRegistrar.getRegistrationId(context);
        if (regId.equals("")) {
            try {
                GCMRegistrar.register(context, GCM_SENDER_ID);
            } catch (Exception ex) {
            }
        } else {
            // Already registered
        }

    }

    public static boolean checkIsGCMServiceAvailable(Context context) {
        try {
            GCMRegistrar.checkDevice(context);
            GCMRegistrar.checkManifest(context);
            return true;
        } catch (Throwable th) {
            return false;
        }
    }

}

我已经附上了我的日志记录信息。

2- 我的谷歌API账户状态如何?

服务 状态 适用于Android的Google云消息传递 没有已知问题

日志记录

模拟器消息

5个回答

20

首先将您的模拟器目标设置为Google API,并向您的模拟器添加一个Google账户。


我已经附上了log cat的截图,还想知道如何在模拟器上添加Google账户。 - egydeveloper
请确保您正在使用针对Google API的模拟器目标。 - Prachi
1
您可以在模拟器上通过"设置"->"账户与同步"添加帐户,在那里您有一个"添加帐户"选项。 - Prachi
com.think.GCMTest 将是我的包名。 - egydeveloper
不,那只是一个示例,在您的清单中,您已经注释了android:enabled="true" ,请取消注释它。 - Prachi
显示剩余4条评论

7

将Google账号添加到模拟器中:
设置->帐户和同步->添加帐户(来自@curious_mind的评论)

如果模拟器运行的是Android 4.0.4或更高版本,则此步骤是可选的,因为GCM不需要从此版本开始使用账户。

对我来说,这解决了问题。


1
你可以使用模拟器来实现GCM功能,但需要注意以下几点:1)模拟器版本应为2.2或以上,且包含Google API(必须)。2)创建AVD模拟器后,在模拟器中按照以下步骤操作:1)进入“设置”-->“帐户与同步”,2)添加Google帐户并开启同步。3)在清单文件中定义互联网权限。
<manifest xlmns:android...>
 ...
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

4.) 在清单文件中定义与GCM相关的权限。

完成此步骤后,您应该准备好注册GCM并接收来自它的消息。


我不理解你的解决方案。能否再读一遍这篇帖子?谢谢。 - user3402040

1

你可以使用这个模拟器来测试你的应用程序,我认为它会很好地工作。


请使用以下链接下载模拟器:http://www.mediafire.com/?2sr9d3hz44pjodc - Girish Bhutiya

0

前往您的Android SDK文件夹,打开SDK Manager并在“Extras”部分下安装Google Cloud Messaging for Android Library。(如果您没有看到Google Cloud Messaging for Android Library,请将SDK管理器更新到最新版本)

安装库后,它将在Andoird_SDK_Folder\extras\google\gcm\gcm-client\dist中创建gcm.jar文件。稍后,您需要将此.jar文件添加到您的Android项目中。

首先将模拟器目标设置为Google API。

启动模拟器后,按菜单按钮进入“设置”。选择“帐户和同步”,然后按“添加帐户”按钮并添加Google帐户。

最后,在模拟器上测试您的应用程序。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接