[英]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.