Вопрос

I have implemented GCM successfully in almost all my app so that I can send notifications to my users. They are working good however now I want my user to decide whether they want notifications or not. Let's say there will be a toggle button which determines whether the device will be registered or unregistered.

I tried the below codes to unregister but with no success, I am still able to receive the notifications

GCMRegistrar.setRegisteredOnServer(context, false);

And

GCMRegistrar.unregister(this)

I can go with work arounds like I can put a flag of false on my server and so notification will not be sent at the point of origin or I wont listen to the notification in onMessage() if user disable option to receive notification however I don't want that.

I want that GCMRegistrar will be unregistered and the service (GCMBaseIntentService) will be stopped.

Это было полезно?

Решение

create a static handler in your class where you are registering gcm

    static Handler mHandler;

and in your oncreate

mHandler = new Handler()
    {
        public void handleMessage(android.os.Message msg) 
        {
            super.handleMessage(msg);
            if (msg.what == 0)
            {

                GCMRegistrar.unregister(context);

            }


        }
    };

now send message to handler to unregister this gcm like this.

        Message msg=MainActivity.mHandler.obtainMessage();
            msg.what=0;
            MainActivity.mHandler.sendMessage(msg);

and this will fire handler of your mainActivity to unregister gcm.

try it ll help you.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top