Android 以编程方式停止GcmListenerService
我正在使用GCM。我想停止GcmListenerService,以便停止应用程序接收不需要的通知 我尝试了以下方法来关闭服务,但似乎没有效果Android 以编程方式停止GcmListenerService,android,service,google-cloud-messaging,Android,Service,Google Cloud Messaging,我正在使用GCM。我想停止GcmListenerService,以便停止应用程序接收不需要的通知 我尝试了以下方法来关闭服务,但似乎没有效果 Intent intent = new Intent(this, MyGcmListenerService.class); stopService(intent); 这是我的服务课 public class MyGcmListenerService extends GcmListenerService { private static fina
Intent intent = new Intent(this, MyGcmListenerService.class);
stopService(intent);
这是我的服务课
public class MyGcmListenerService extends GcmListenerService
{
private static final String TAG = "MyGcmListenerService";
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
String duration = data.getString("duration");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
sendNotification(message,duration);
}
// [END receive_message]
private void sendNotification(String message, String duration) {
new DisplayNotification(this).makeGCMNotification("Event Name", "Event Start Time", 1);
}
}
而且在舱单上
<service
android:name=".Utilities.GCM.MyGcmListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
</intent-filter>
</service>
停止服务没有帮助,因为每当收到新消息时,GCMReceiver总是调用startWakefulService
。由于GCMReceiver类实际上不是我们应该编辑的对象,因此我建议您保留一个布尔值或设置一个首选项,以知道何时应该读取/处理收到的消息。基于此,您可以让消息在后台接收,而实际上在应用程序/前台不执行任何操作。您可以使用两种方法从GCM取消订阅:deleteToken()
和deleteInstanceID()
。这两种方法都会生成IOException,因此也必须处理它们
请看。我只是不想在应用程序中的某个时候收到通知。请帮忙谢谢:)我想到了这个方法。但我认为这并不是停止接收推送通知的真正合适的方法。您能告诉我有没有任何可能的方法可以从GCM服务器本身注销设备?假设是您的服务器发送消息,您可以发出http请求或发送上游消息,告诉您的服务器停止发送到此设备。一种更暴力的方法是调用deleteToken()