简体   繁体   English

Google Cloud Messaging等待注册

[英]Google Cloud Messaging Waiting For Register

In my GCM project, I want to wait for AsyncTask until the register returns. 在我的GCM项目中,我想等待AsyncTask直到寄存器返回。

I've tried to use synchronized and CountDownLatch to wait in the main thread. 我试着使用synchronizedCountDownLatch在主线程等待。

However, every time I wait, my app exits with exception SERVICE_NOT_AVAILABLE on register method. 但是,每次我等待时,我的应用程序都会退出,并在注册方法上出现SERVICE_NOT_AVAILABLE异常。

Why does this exception occur? 为什么会发生此异常? I get a perfectly fine registration ID normally. 我通常会得到一个非常好的注册ID。 Also, the waiting process is successful without the register method. 同样,没有注册方法,等待过程也会成功。

Thx for your help in advance. 谢谢您的帮助。

The SERVICE_NOT_AVAILABLE error comes often with the GCM register() method. SERVICE_NOT_AVAILABLE错误通常与GCM register()方法一起出现。 You can try the following: 您可以尝试以下方法:

  1. Add REGISTRATION to the GcmBroadcastReceiver Intent Filter in AndroidManifest.xml file 在AndroidManifest.xml文件中将REGISTRATION添加到GcmBroadcastReceiver目的过滤器

    <receiver android:name=".GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="YOUR_PACKAGE_NAME" /> </intent-filter> </receiver>

  2. Obtain the Registration ID in GcmBroadcastReceivers onReceive method 在GcmBroadcastReceivers onReceive方法中获取注册ID

    @Override

    public void onReceive(Context context, Intent intent) {

     String regId = intent.getExtras().getString("registration_id"); 

    }

Hope this works for you. 希望这对您有用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM