2015-08-26 98 views
-1

你好,我的問題是這樣的。 我有一個Android應用接收Google Cloud Messaging的dotifiche,直到手機屏幕變爲接收通知,但只要我關掉它沒有得到任何東西,你應該公開代碼,現在你有一些想法?Cloud Messaging for Android後臺listenig

回答

0
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.lucabigoni.progettoclientserverolbia" > 

<uses-sdk 
    android:minSdkVersion="17" 
    android:targetSdkVersion="17" /> 

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 

<uses-permission android:name="android.permission.VIBRATE" /> 

<permission 
    android:name="com.example.lucabigoni.progettoclientserverolbia.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 

<uses-permission android:name="com.example.lucabigoni.progettoclientserverolbia.C2D_MESSAGE" /> 

<action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 

<uses-permission android:name="android.permission." /> 

<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".Activity.SplashScreen" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:theme="@android:style/Theme.Black.NoTitleBar" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 



    <activity 
     android:name=".Activity.Prova" 
     android:label="@string/ciao" > 
    </activity> 
    <activity 
     android:name=".Activity.MainActivity" 
     android:label="main" > 
    </activity> 
    <activity 
     android:name=".Activity.manu" 
     android:label="@string/title_activity_manu" > 
    </activity> 
    <activity 
     android:name=".Activity.Visualizza" 
     android:label="@string/title_activity_visualizza" > 
    </activity> 

    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
    <meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="AIzaSyADrZavHTaGfd8_pTyqOMQLMQg2jfJT_S4" /> 

    <activity 
     android:name=".Activity.mapOlbia" 
     android:label="@string/title_activity_map_olbia" > 
    </activity> 
    <activity 
     android:name=".Activity.Immagine_nave" 
     android:label="@string/title_activity_immagine_nave" > 
    </activity> 
    <activity 
     android:name=".Activity.Splash_Scree_caricamento_Immagine" 
     android:label="@string/title_activity_splash__scree_caricamento__immagine" > 
    </activity> 
    <activity 
     android:name=".Activity.Splash_screen_caricamento_dati" 
     android:label="@string/title_activity_splash_screen_caricamento_dati" > 
    </activity> 
    <activity 
     android:name=".Activity.AscoltoNave" 
     android:label="@string/title_activity_ascolto_nave" > 
    </activity> 
    <activity 
     android:name=".Activity.Scegli" 
     android:label="@string/title_activity_scegli" > 
    </activity> 
    <activity 
     android:name=".Activity.SceltaTipologia" 
     android:label="@string/title_activity_scelta_tipologia" > 
    </activity> 

    <receiver 
     android:name=".ricevitorenotifiche.NotificationReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 

      <category android:name="com.android.recognition" /> 

      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 

      <category android:name="it.html.googleservices.push" /> 
     </intent-filter> 
    </receiver> 

    <intentservice 
     android:name=".ricevitorenotifiche.GcmIntentService" 
     android:enabled="true" 
     android:exported="false" 
     android:process=":questo" > 
    </intentservice> 

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

      <category android:name="com.thanksandroid.example.gcmdemo" /> 
     </intent-filter> 
    </receiver> 
</application> 

import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v4.content.WakefulBroadcastReceiver; 
import android.util.Log; 


public class GcmBroadcastReceiver extends WakefulBroadcastReceiver{ 
@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    // Explicitly specify that GcmIntentService will handle the intent. 
     Log.e("Ricevuto","CIAO SONO PRONTO"); 
    ComponentName comp = new ComponentName(context.getPackageName(), 
      GcmIntentService.class.getName()); 
    // Start the service, keeping the device awake while it is launching. 
    startWakefulService(context, (intent.setComponent(comp))); 
    setResultCode(Activity.RESULT_OK); 

} 

} 


package com.example.lucabigoni.progettoclientserverolbia.ricevitorenotifiche; 

import com.example.lucabigoni.progettoclientserverolbia.Activity.MainActivity; 
import com.example.lucabigoni.progettoclientserverolbia.R; 
import com.google.android.gms.gcm.GoogleCloudMessaging; 

import android.app.Activity; 
import android.app.IntentService; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.SystemClock; 
import android.support.v4.app.NotificationCompat; 
import android.support.v4.content.WakefulBroadcastReceiver; 
import android.util.Log; 

public class GcmIntentService extends IntentService /*implements   WakefulBroadcastReceiver */{ 
Context context; 
public static int NOTIFICATION_ID = 1; 
private NotificationManager mNotificationManager; 
NotificationCompat.Builder builder; 
public static final String TAG = "GCM Demo"; 

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

}*/ 
@Override 
protected void onHandleIntent(Intent intent) { 
    // TODO Auto-generated method stub 
    Bundle extras = intent.getExtras(); 
    String msg = intent.getStringExtra("message"); 
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
    String messageType = gcm.getMessageType(intent); 
Log.e("qua arrivato","qua"); 
    if (!extras.isEmpty()) { 

     if (GoogleCloudMessaging. 
       MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { 
      sendNotification("Send error: " + extras.toString()); 
     } else if (GoogleCloudMessaging. 
       MESSAGE_TYPE_DELETED.equals(messageType)) { 
      sendNotification("Deleted messages on server: " + 
        extras.toString()); 
      // If it's a regular GCM message, do some work. 
     } else if (GoogleCloudMessaging. 
       MESSAGE_TYPE_MESSAGE.equals(messageType)) { 
      // This loop represents the service doing some work. 
      for (int i=0; i<5; i++) { 
       Log.i(TAG, "Working... " + (i+1) 
         + "/5 @ " + SystemClock.elapsedRealtime()); 
       try { 
        Thread.sleep(500); 
       } catch (InterruptedException e) { 
       } 
      } 
      Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime()); 
      // Post notification of received message. 
      //sendNotification("Received: " + extras.toString()); 
      sendNotification(msg); 
      Log.i(TAG, "Received: " + extras.toString()); 
     } 
    } 
    GcmBroadcastReceiver.completeWakefulIntent(intent); 
} 
private void sendNotification(String msg) { 
    mNotificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 
    Log.e("qua arrivato","qua2"); 
    Intent myintent = new Intent(this, MainActivity.class); 
    myintent.putExtra("message", msg); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
      myintent, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setContentTitle("GCM Notification") 
        .setStyle(new NotificationCompat.BigTextStyle() 
          .bigText(msg)) 
        .setContentText(msg); 

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
    NOTIFICATION_ID++; 
} 

}