Google Cloud Messaging注册失败

 Jason 发布于 2023-02-07 17:29

问题解决了 更新:

我解决了这个问题.问题在于方法registerGCMInBackground(). GoogleCloudMessaging.getInstance()期待ApplicationContext.

    private void registerGCMInBackground(final String userId) {
    new AsyncTask() {
        @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;
        }

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

我在GCM(Google Cloud Messaging)注册我的Android设备时遇到问题.我正在使用Google Developers中的示例.通过使用Google Developer Example,我确保了以下事项:

添加最新的Play服务库

添加最新的Play服务JAR

正确的清单文件

在设备上安装了Play Services APK

使用项目编号作为SENDER_ID

这个问题似乎与此问题相同.不幸的是仍然没有回答 我通过以下行获得Null Pointer Exception:

regid = gcm.register(SENDER_ID);

这是我的问题代码: 示例活动

     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() {
            @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);
    }

这是我的控制台输出: 控制台输出

这个问题我已经整整两天了.:( 非常感谢你!

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有