2016-02-19 209 views
-3

我遵循google的文檔來實現推送通知。 當應用程序打開工作正常,但是當我關閉應用程序不會收到通知。當應用程序關閉時的Android推送通知

下面是代碼: 艙單

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.VIBRATE"/> 
<uses-permission android:name="it.bsdsoftware.cnabologna.permission.C2D_MESSAGE" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<permission 
     android:name="it.bsdsoftware.cnabologna.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

<meta-data 
    android:name="com.google.android.gms.version" 
    android:value="@integer/google_play_services_version"/> 
<application> 
    ... 
    <service android:name=".notifiche.RegistrationIntentService" 
      android:exported="false" > 
    </service> 
    <service android:name=".notifiche.MyGcmListenerService" 
      android:exported="false" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
     </intent-filter> 
    </service> 
    <service android:name=".notifiche.MyInstanceIDListenerService" 
      android:exported="false" > 
     <intent-filter> 
      <action android:name="com.google.android.gms.iid.InstanceID" /> 
     </intent-filter> 
    </service> 
    <receiver android:name=".notifiche.GcmBroadcastReceiver" 
       android:exported="true" 
       android:permission="com.google.android.c2dm.permission.SEND" > 
     <intent-filter> 
      <!-- Receives the actual messages. --> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION"/> 
      <category android:name="it.bsdsoftware.cnabologna" /> 
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
     </intent-filter> 
    </receiver> 
</application> 

RegistrationIntentService.java

public class RegistrationIntentService extends IntentService { 

    private static final String TAG = "RegIntentService"; 

    public RegistrationIntentService() { 
     super(TAG); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     try { 
      InstanceID instanceID = InstanceID.getInstance(this); 
      String token = instanceID.getToken(getString(R.string.notifiche_push_sender_id), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); 
      Log.i(TAG, "GCM Registration Token: " + token); 
      sendRegistrationToServer(token); 
     } catch (Exception e) { 
      Log.d(TAG, "Failed to complete token refresh", e); 
     } 
    } 

    private void sendRegistrationToServer(String token) { 
     OperazioneInvioToken op = new OperazioneInvioToken(token); 
     WebServiceTask ws = new WebServiceTask(op, this, false); 
     ws.execute(); 
    } 

} 

MyGcmListenerService.java

public class MyGcmListenerService extends GcmListenerService { 

    private static final String TAG = "MyGcmListenerService"; 

    @Override 
    public void onMessageReceived(String from, Bundle data) { 

     Bundle notification = data.getBundle("notification"); 

     String title = ""; 
     String message = ""; 

     if(notification!=null){ 
      title = notification.getString("title"); 
      message = notification.getString("body"); 
     } 

     NotificationUtility.showNotification(title, message); 
     NotificationUtility.setBadge(1); 
    } 
} 

MyInstanceIDListenerService.java

public class MyInstanceIDListenerService extends InstanceIDListenerService { 

    /** 
    * Called if InstanceID token is updated. This may occur if the security of 
    * the previous token had been compromised. This call is initiated by the 
    * InstanceID provider. 
    */ 
    @Override 
    public void onTokenRefresh() { 
     // Fetch updated Instance ID token and notify our app's server of any changes (if applicable). 
     Intent intent = new Intent(this, RegistrationIntentService.class); 
     startService(intent); 
    } 
} 

GcmBroadcastReceiver.java

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Explicitly specify that GcmIntentService will handle the intent. 
     ComponentName comp = new ComponentName(context.getPackageName(), MyGcmListenerService.class.getName()); 
     // Start the service, keeping the device awake while it is launching. 
     startWakefulService(context, (intent.setComponent(comp))); 
     setResultCode(Activity.RESULT_OK); 
    } 
} 

當應用程序是GcmListenerService類開放onMessageReceived被稱爲但是當應用程序被關閉不叫。

我做錯了什麼? 感謝

回答

0

我通過更新從com.google.android.gms播放服務解決:發揮服務:8.3.0至com.google.android.gms:發揮服務:8.4.0

相關問題