取消注册后GCM仍会收到通知

时间:2013-09-24 10:35:19

标签: java android google-cloud-messaging

我正在使用GCM。它的工作完美,但在取消注册后我仍会收到通知。

这是我的注册:

     // Make sure the device has the proper dependencies.
    GCMRegistrar.checkDevice(context);

    // Make sure the manifest was properly set - comment out this line
    // while developing the app, then uncomment it when it's ready.
    GCMRegistrar.checkManifest(context);

    registerReceiver(mHandleMessageReceiver, new IntentFilter(
            DISPLAY_MESSAGE_ACTION));

    // Get GCM registration id
    final String regId = GCMRegistrar.getRegistrationId(context);

    // Check if regid already presents
    if (regId.equals("")) {
        // Registration is not present, register now with GCM           
        GCMRegistrar.register(context, SENDER_ID);
    } else {
        // Device is already registered on GCM
        if (GCMRegistrar.isRegisteredOnServer(context)) {
            // Skips registration.              
            Toast.makeText(context, "Already registered with GCM", Toast.LENGTH_LONG).show();
        } else {
            // Try to register again, but not in the UI thread.
            // It's also necessary to cancel the thread onDestroy(),
            // hence the use of AsyncTask instead of a raw thread.

            mRegisterTask = new AsyncTask<Void, Void, Void>() {

                @Override
                protected Void doInBackground(Void... params) {
                    // Register on our server
                    // On server creates a new user
                    ServerUtilities.register(context, user, pass, regId);
                    return null;
                }

                @Override
                protected void onPostExecute(Void result) {
                    mRegisterTask = null;
                }

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

从我尝试从GCM取消注册的不同活动:

GCMRegistrar.unregister(getApplicationContext());
    GCMRegistrar.onDestroy(getApplicationContext());

之后我仍然收到通知:(

1 个答案:

答案 0 :(得分:0)

首先,不推荐使用GCMRegistrar

其次,unregister()表示此设备不应再次接收消息。经常注册和取消注册不是预期的应用程序行为。如果您想停止接收消息,告诉您的应用服务器停止发送消息

相关问题