简体   繁体   English

如何停止GCMBaseIntentService以便不会收到通知?

[英]How to stop GCMBaseIntentService so that notification will not get received?

I have implemented GCM successfully in almost all my app so that I can send notifications to my users. 我已经在几乎所有应用程序中成功实现了GCM,因此我可以向用户发送通知。 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. 我可以使用变通办法,例如可以在服务器上放置false标志,因此通知将不会在原始点发送,或者如果用户禁用了接收通知的选项,则不会在onMessage()监听通知,但是我不会不想那样。

I want that GCMRegistrar will be unregistered and the service (GCMBaseIntentService) will be stopped. 我希望GCMRegistrar将被注销,并且该服务(GCMBaseIntentService)将被停止。

create a static handler in your class where you are registering gcm 在您注册gcm的类中创建一个静态处理程序

    static Handler mHandler;

and in your oncreate 并在您的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. 现在将消息发送到处理程序,以注销该gcm,如下所示。

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

and this will fire handler of your mainActivity to unregister gcm. 这将触发您的mainActivity处理程序注销gcm。

try it ll help you. 尝试它会帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在gcmbaseintentservice中获取位置 - how to get location in gcmbaseintentservice 如何从GCMBaseIntentService中的onRegistered()方法获取Activity对象的引用 - How to get reference of Activity object from onRegistered() method in GCMBaseIntentService GCM通知GCMBaseIntentService方法generateNotification()无法正确运行 - GCM Notification the GCMBaseIntentService method generateNotification() not run properlly 如何收到Android中收到的通知的回电? - How do I get a call back for notification received in android? 如何获取 Anroid 中所有应用程序收到的通知计数 - How to get count of received notification for All applications in Anroid 在Android中获取通知的接收时间 - Get Notification received time in Android 如何用等效的GoogleCloudMessaging方法替换GCMBaseIntentService - How to replace GCMBaseIntentService with an equivalent GoogleCloudMessaging methods 如何处理收到的信号通知 - How to handle oneSignal notification received 收到推送通知后强制停止后Whatsapp服务重新启动 - Whatsapp service restarts after force stop when push notification received 应用程序停止时如何在Android中获取本地通知? - How to get local notification in Android while Application stop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM