如何在Android中启用和禁用GCM? [英] How to enable and disable GCM in android?

查看:305
本文介绍了如何在Android中启用和禁用GCM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用GCM进行推送通知。我有一个屏幕,用户可以启用或禁用通知。

任何人都可以请告诉我如何以编程方式启用和禁用GCM。



我已经从这篇文章中看到了下面的代码 Android GCM启用/禁用,但它是不清楚 receiver

  PackageManager pm = getApplicationContext()。getPackageManager(); 
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);

预先致谢。

解决方案

如果您指的是Android设置(如果我没有记错的话,在Android 4.1中添加)允许用户为应用程序启用/禁用通知,但我认为您不能以编程方式更改该设置。

但是,如果您在自己的应用程序中引用设置屏幕,则可以通过从GCM中取消注册来禁用通知( GoogleCloudMessaging .unregister )。这应该会阻止邮件到达该设备上的应用程序,直到您再次注册到GCM。



或者,如果您希望应用继续收到邮件但不再显示邮件对用户而言,您可以在共享首选项中存储标志,指示是否启用GCM。然后,处理传入的GCM消息的代码将检查该标志以决定如何处理该消息。


I am using GCM for push notifications in my application. I have a screen where the user has the facility to enable or disable notifications.
Can anyone please tell me how to enable and disable GCM programmatically.

I had seen the below code from this post Android GCM Enable/Disable but it is not clear about the receiver.

PackageManager pm = getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);

Thanks in advance.

解决方案

If you are referring to the Android setting (adding in Android 4.1 if I remember correctly) that allows a user to enable/disable notifications for an application, I don't think you can change that programmatically.

If, however, you are referring to a settings screen within your own app, you can disable the notifications by un-registering from GCM (GoogleCloudMessaging.unregister). This should stop messages from arriving to your app on that device until you register to GCM again.

Or, if you want your app to keep receiving the messages but stop showing them to the user, you can store a flag in shared preferences which indicates if GCM is enabled. Then the code that handles incoming GCM messages will check this flag to decide what to do with the message.

这篇关于如何在Android中启用和禁用GCM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆