谷歌云信息登记失败 [英] Google Cloud Messaging register fails

查看:162
本文介绍了谷歌云信息登记失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题解决了 更新:

我解决了这个问题。 问题出在方法registerGCMInBackground()。 GoogleCloudMessaging.getInstance()预计的ApplicationContext。

I solved this Problem. The problem is in method registerGCMInBackground(). GoogleCloudMessaging.getInstance() expects the ApplicationContext.

    private void registerGCMInBackground(final String userId) {
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            Log.d("Register GCM", "started");
            try {
                if (gcm == null) {
                    //PROBLEM SOLVED HERE
                    gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
                    Log.d("GCM", gcm.toString());
                }



                regid = gcm.register(SENDER_ID); //////NULL POINTER EXCEPTION
                msg = "Device registered, registration ID=" + regid;



                // You should send the registration ID to your server over HTTP
                sendRegistrationIdToBackend(userId, regid);

                // For this demo: we don't need to send it because the device will send
                // upstream messages to a server that echo back the message using the
                // 'from' address in the message.

                // Persist the regID - no need to register again.
                storeRegistrationId(context, regid);
            } catch (IOException ex) {
                return "Error :" + ex.getMessage();
                // If there is an error, don't just keep trying to register.
                // Require the user to click a button again, or perform
                // exponential back-off.
            }
            return regid;
        }

--------解决的问题--------------

-------- Solved Problem--------------

我有问题,我的注册Android设备的GCM(谷歌云消息传递)。我使用的是从谷歌开发者的例子。随着使用谷歌开发的例子我保证以下几点:

I have problems with registering my Android device at GCM (Google Cloud Messaging). I'm using the example from Google Developers. With the use of the Google Developer Example I ensured the following things:

  • 在加入最新的播放服务资料库
  • 在加入最新的播放服务JAR
  • 在正确的清单文件
  • 在安装装置,具有播放服务APK
  • 使用项目编号为SENDER_ID

这似乎是这样的问题同样的问题。遗憾的是仍然没有回答。 我得到一个空指针异常的下面一行: REGID = gcm.register(SENDER_ID);

It seems to be the same issue like this Question. Unfortunately still not answered. I'm getting a Null Pointer Exception by the following line: regid = gcm.register(SENDER_ID);

下面是我的问题code: 样品活动

Here is my Problem Code: Sample Activity

     static final String TAG = "GCM";
    public static final String EXTRA_MESSAGE = "message";
    public static final String PROPERTY_REG_ID = "registration_id";
    private static final String PROPERTY_APP_VERSION = "appVersion";
    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    String SENDER_ID = "xxxxxxxxxxxxxxx";
    GoogleCloudMessaging gcm;
    AtomicInteger msgId = new AtomicInteger();
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.registration);

            final Button register = (Button) findViewById(R.id.btn_register);

            register.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                     EditText mobilePhoneNumber = (EditText) findViewById(R.id.et_phonenumber);

                    if(!mobilePhoneNumber.getText().toString().isEmpty()){
                    User user = new User();
                    user.setMobilePhoneNumber(mobilePhoneNumber.getText().toString());

                     EditText firstName = (EditText) findViewById(R.id.et_firstName);
                    user.setFirstName(firstName.getText().toString());

                     EditText lastName = (EditText) findViewById(R.id.et_lastName);
                    user.setLastName(lastName.getText().toString());

                     EditText email = (EditText) findViewById(R.id.et_email);
                    user.setEmail(email.getText().toString());
                    Log.d("email: ", user.getEmail().toString());

                    if (android.os.Build.VERSION.SDK_INT>14) {
                        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                                .permitAll().build();
                        StrictMode.setThreadPolicy(policy);
                    }

                    UserService userService = new UserService();
                    String path = userService.createNewUser(user);




                    Log.d("Location: ", path);
                    Toast.makeText(getApplicationContext(), 
                            path, Toast.LENGTH_LONG).show();

                    String userID = path.replace("/user/", "");
                    userID = userID.replace("/users/", "");
                    Log.d("UserID:::", userID);
                    Log.d("User Service", "register in Background started");
                    if(checkPlayServices()){
                    registerGCMInBackground(userID);
                    }

                    }
                    else{
                        Toast.makeText(getApplicationContext(), 
                                "Please enter your Mobile Phone Number", Toast.LENGTH_LONG).show();
                    }           
                }
            });

    private void registerGCMInBackground(final String userId) {
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                String msg = "";
                Log.d("Register GCM", "started");
                try {
                    if (gcm == null) {
                        gcm = GoogleCloudMessaging.getInstance(context);
                        Log.d("GCM", gcm.toString());
                    }



                    regid = gcm.register(SENDER_ID); //////NULL POINTER EXCEPTION
                    msg = "Device registered, registration ID=" + regid;



                    // You should send the registration ID to your server over HTTP
                    sendRegistrationIdToBackend(userId, regid);

                    // For this demo: we don't need to send it because the device will send
                    // upstream messages to a server that echo back the message using the
                    // 'from' address in the message.

                    // Persist the regID - no need to register again.
                    storeRegistrationId(context, regid);
                } catch (IOException ex) {
                    return "Error :" + ex.getMessage();
                    // If there is an error, don't just keep trying to register.
                    // Require the user to click a button again, or perform
                    // exponential back-off.
                }
                return regid;
            }

            @Override
            protected void onPostExecute(String msg) {

            }
        }.execute(null, null, null);
    }

下面是我的控制台输出: <一href="https://www.dropbox.com/s/sbj1qrwqy6nzgtl/Bildschirmfoto%202013-12-28%20um%2023.12.42%20copy.png">Console输出

Here is my Console Output: Console Output

我处理,因为这个问题了整整两天。 :( 非常感谢你!

I'm dealing since two full days with this issue. :( Thank you very much!

推荐答案

我解决了这个问题。 问题出在方法registerGCMInBackground()。 GoogleCloudMessaging.getInstance()预计的ApplicationContext。

I solved this Problem. The problem is in method registerGCMInBackground(). GoogleCloudMessaging.getInstance() expects the ApplicationContext.

    private void registerGCMInBackground(final String userId) {
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            Log.d("Register GCM", "started");
            try {
                if (gcm == null) {
                    //PROBLEM SOLVED HERE
                    gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
                    Log.d("GCM", gcm.toString());
                }



                regid = gcm.register(SENDER_ID); //////NULL POINTER EXCEPTION
                msg = "Device registered, registration ID=" + regid;



                // You should send the registration ID to your server over HTTP
                sendRegistrationIdToBackend(userId, regid);

                // For this demo: we don't need to send it because the device will send
                // upstream messages to a server that echo back the message using the
                // 'from' address in the message.

                // Persist the regID - no need to register again.
                storeRegistrationId(context, regid);
            } catch (IOException ex) {
                return "Error :" + ex.getMessage();
                // If there is an error, don't just keep trying to register.
                // Require the user to click a button again, or perform
                // exponential back-off.
            }
            return regid;
        }

这篇关于谷歌云信息登记失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆