2013-07-05 48 views
0

我將GCM與Android應用程序集成以接收推送通知。一切正常,我可以收到通知。問題是,即使應用程序未運行,但我需要接收它們時,我收到這些通知運行GCM - 以編程方式註冊廣播接收器時獲取SERVICE_NOT_AVAILABLE

所以我認爲,如果我將在應用程序中以編程方式註冊廣播接收器,並在onDestroy()中取消註冊它將完成這項工作。
但是,這不起作用,當試圖獲得註冊ID它返回SERVICE_NOT_AVAILABLE。

這是明顯的作品申報的廣播接收器,但我收到通知,所有的時間:

<receiver android:name=".service.MyBroadcastReceiver" 
        android:permission="com.google.android.c2dm.permission.SEND"> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE"/> 
       <category android:name="com.testgcm"/> 
      </intent-filter> 
</receiver> 

這就是我如何編程方式創建,但收到上述錯誤:

receiver = new MyBroadcastReceiver(); 
IntentFilter filter = new IntentFilter(); 
filter.addAction("com.google.android.c2dm.intent.RECEIVE"); 
filter.addCategory("com.testgcm"); 
registerReceiver(receiver, filter, "com.google.android.c2dm.permission.SEND", null); 

你能告訴我我做錯了什麼? 或者也許有更好的方式使得只有在應用程序運行時才能接收通知?

回答

1

事實證明,這與我以編程方式註冊BroadcastReceiver這一事實有關,但事實上它代表了註冊失敗操作。

這裏的文件說什麼:

When the application receives a com.google.android.c2dm.intent.REGISTRATION intent with the error extra set as SERVICE_NOT_AVAILABLE, it should retry the failed operation (register or unregister).

In the simplest case, if your application just calls register and GCM is not a fundamental part of the application, the application could simply ignore the error and try to register again the next time it starts. Otherwise, it should retry the previous operation using exponential back-off. In exponential back-off, each time there is a failure, it should wait twice the previous amount of time before trying again. If the register (or unregister) operation was synchronous, it could be retried in a simple loop. However, since it is asynchronous, the best approach is to schedule a pending intent to retry the operation.

相關問題