2016-09-14 52 views
16

我從Android Studio的Android監視器收到該錯誤。當我通過GCM發送推送通知,在真實設備中,並且應用尚未啓動或被迫停止時,會出現此錯誤。昨天一切正常,今天根本不工作(只有當應用程序在後臺或前臺運行時纔有效)。錯誤廣播意圖回調:結果= CANCELED forIntent {act = com.google.android.c2dm.intent.RECEIVE pkg = com.flagg327.guicomaipu(有額外)}

我認爲這可能是一個AndroidManifest錯誤,但我厭倦了尋找問題,找不到任何東西。

清單

<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.flagg327.guicomaipu"> 

    ... 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

     ... 

     <!--GOOGLE CLOUD MESSAGE--> 
     <receiver 
      android:name="com.google.android.gms.gcm.GcmReceiver" 
      android:exported="true" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED"/> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <!-- for Gingerbread GSF backward compat --> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
       <category android:name="com.flagg327.guicomaipu" /> 
      </intent-filter> 
     </receiver> 

     <service 
      android:name="com.flagg327.guicomaipu.gcm.RegistrationService" 
      android:exported="false" /> 

     <service 
      android:name="com.flagg327.guicomaipu.gcm.TokenRefreshListenerService" 
     android:exported="false"> 
      <intent-filter> 
       <action 
        android:name="com.google.android.gms.iid.InstanceID" /> 
      </intent-filter> 
     </service> 

     <service 
      android:name="com.flagg327.guicomaipu.gcm.NotificacionsListenerService" 
     android:exported="false" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      </intent-filter> 
     </service> 

    </aplication> 

    <permission 
     android:name="com.flagg327.guicomaipu.C2D_MESSAGE" 
      android:protectionLevel="signature" /> 
    <uses-permission android:name="com.flagg327.guicomaipu.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

TokenRefreshListenerService.java

每天登記 '令牌' 更新。因此,每個使用GCM的Android應用都必須有一個InstanceIDListenerService來管理這些更新。

public class TokenRefreshListenerService extends InstanceIDListenerService{ 

    @Override 
    public void onTokenRefresh() { 
     // Launch the registration process. 
     Intent i = new Intent(this, RegistrationService.class); 
     startService(i); 
    } 
} 

NotificacionsListenerService.java

GCM自動顯示的推送通知,但只有當相關聯的應用程序有一個GCMListenerService

public class NotificacionsListenerService extends GcmListenerService { 

    @Override 
    public void onMessageReceived(String from, Bundle data) { 
     Log.d("A", "onMessageReceived()"); 

     // Do something 

    } 
} 

RegistrationService.java

GCM識別Android設備使用情況g註冊卡('令牌')。我的應用程序應該能夠從安裝它的每個Android設備註冊。

public class RegistrationService extends IntentService { 

    /** 
    * Constructor 
    */ 
    public RegistrationService() { 
     super("RegistrationService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     // Generate or download the registration 'token'. 
     InstanceID myID = InstanceID.getInstance(this); 

     String registrationToken = null; 
     try { 
      // Get the registration 'token'. 
      registrationToken = myID.getToken(
        getString(R.string.gcm_defaultSenderId), 
        GoogleCloudMessaging.INSTANCE_ID_SCOPE, 
        null 
      ); 

      // Subscribe to a topic. The app is able now to receive notifications from this topic. 
      GcmPubSub subscription = GcmPubSub.getInstance(this); 
      subscription.subscribe(registrationToken, "/topics/guico_maipu_topic", null); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     Log.e("Registration Token", registrationToken); 
    } 
} 

錯誤

,當我通過Python發送推送通知,將出現此錯誤。

09-13 21:21:44.800 1851-1851/? W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.flagg327.guicomaipu (has extras) } 

昨天工作...任何想法?比你的時間。

+0

從此[線程]根據(https://groups.google.com/forum/#!msg/android-gcm/MqVlA3Sj26c/6O4TdU1pm0UJ),當接收應用程序是在停止發生此問題設備上的狀態(例如,通過設置強制停止)。手動啓動時,它只會再次開始接收消息。你也可以檢查這[相關SO帖子](http://stackoverflow.com/questions/11902947/broadcast-intent-callback-result-cancelled-forintent)。 – abielita

+0

Sloved檢查此..https://stackoverflow.com/a/45810771/1993001 –

回答

7

所以......我解決了這個問題。問題在於如果應用程序強制關閉或者設備啓動後應用程序從未打開過,則設備未註冊爲接收GCM。解決方案很簡單,在手機啓動時註冊設備。爲此,我實施了一個BroadcastReceiver,並在其中啓動了一個流程註冊。

的修改:

加入AndroidManifest

<receiver android:name="com.flagg327.guicomaipu.gcm.OnBootBroadcastReceiver"> 
     <intent-filter > 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 

      <category android:name="android.intent.category.HOME" /> 
     </intent-filter> 
    </receiver> 

OnBootBroadcastReceiver.java

public class OnBootBroadcastReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     Intent i = new Intent("com.flagg327.guicomaipu.gcm.RegistrationService"); 
     i.setClass(context, RegistrationService.class); 
     context.startService(i); 
    } 
} 

因此,在引導時,設備會註冊到GCM服務器中去能夠從我的服務器接收任何推送通知。我希望它是有用的。

+0

謝謝@ flagg327,我是同樣的問題,並通過您的答案解決它。 –

+4

當我們強制殺死應用程序時,這是否工作? –

7

我得到了同樣的錯誤

09-13 21:21:44.800 1851-1851/? W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.XXX.XXX (has extras) } 

查殺應用之後。

經過一番研究,我意識到,這只是一個「調試問題」。即使應用程序已關閉,「已簽名APK」也能正確處理廣播。

希望它有幫助!

+5

它沒有爲我工作。 :( –

+0

請鏈接您的資源的來源 – natanavra

+0

@natanavra這是什麼意思?完整的源代碼已經在原始問題 - 是不是? – niggeulimann

3

深入探討這種情況後,看起來會發生這種情況,如果您從Android Studio中終止應用程序。

如果您從啓動器中打開應用程序並將其滑開,您的應用程序仍會收到通知。

(在帶有FCM的OnePlus One上測試不是GCM)

+1

這幫了我很多,謝謝。 – gorkem

0

我也遇到過這個問題。當我從堆棧中刪除應用程序時,推送通知未收到。大量谷歌後,我發現很少有手機具有電池優化功能,這將禁用後臺運行的應用程序。當我啓用時,我的應用程序也可以在後臺運行。推送通知正常工作。只有很少的應用程序,如What's application等,只能有默認啓用功能。你可以看看下面這個URL。

https://github.com/firebase/quickstart-android/issues/89