2014-02-02 4 views
1

Я пытаюсь внедрить новую службу GCM для push-уведомлений. Я видел несколько похожих сообщений об этой проблеме, и большинство из них указывают на манифест, который что-то пропустил. Не удалось найти то, что я там не хватает:BroadcastReceiver никогда не вызывается с использованием сервиса GCM

Manifest.xml (Original со всеми permisions и т.д .. в случае, если что-то efecting)

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.catchme2" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="12" 
     android:targetSdkVersion="18" /> 
    <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="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 
    <uses-permission android:name="android.permission.CALL_PHONE" /> 
    <!-- The following two permissions are not required to use 
     Google Maps Android API v2, but are recommended. --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 

    <permission android:name="com.example.catchme2.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 
    <uses-permission android:name="com.example.catchme2.permission.C2D_MESSAGE" /> 


    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true"/> 
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/Theme.Sherlock" 
     android:uiOptions="splitActionBarWhenNarrow" > 
     <activity 
      android:name="com.example.catchme2.FbActivity" 
      android:label="@string/app_name" 
      android:configChanges="orientation|screenSize" 
      android:uiOptions="splitActionBarWhenNarrow"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </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="MyAPIKEY"/> 
     <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/> 
     <activity android:name="com.facebook.LoginActivity"></activity> 
     <activity android:name="MainActivity"></activity> 

     <receiver 
      android:name="com.example.catchme2.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" /> 
       <action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION" /> 
       <category android:name="com.example.catchme2" /> 
      </intent-filter> 
     </receiver> 
     <service android:name="com.example.catchme2.GcmIntentService" 
      android:enabled="true"/> 
    </application> 
</manifest> 

BroadcastReceiver.class

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(), 
       GcmIntentService.class.getName()); 
     // Start the service, keeping the device awake while it is launching. 
     startWakefulService(context, (intent.setComponent(comp))); 
     setResultCode(Activity.RESULT_OK); 
    } 

} 

IntentService.class

public class GcmIntentService extends IntentService { 

    public static final int NOTIFICATION_ID = 1; 
    private NotificationManager mNotificationManager; 
    NotificationCompat.Builder builder; 
    static final String TAG = "GCMDemo"; 

    public GcmIntentService(String name) { 
     super("GcmIntentService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Bundle extras = intent.getExtras(); 
     GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
     // The getMessageType() intent parameter must be the intent you received 
     // in your BroadcastReceiver. 
     String messageType = gcm.getMessageType(intent); 

     if (!extras.isEmpty()) { // has effect of unparcelling Bundle 
      /* 
      * Filter messages based on message type. Since it is likely that GCM 
      * will be extended in the future with new message types, just ignore 
      * any message types you're not interested in, or that you don't 
      * recognize. 
      */ 
      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(5000); 
        } catch (InterruptedException e) { 
        } 
       } 
       Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime()); 
       // Post notification of received message. 
       sendNotification("Received: " + extras.toString()); 
       Log.i(TAG, "Received: " + extras.toString()); 
      } 
     } 
     // Release the wake lock provided by the WakefulBroadcastReceiver. 
     GcmBroadcastReceiver.completeWakefulIntent(intent); 

    } 

    private void sendNotification(String msg) { 
     mNotificationManager = (NotificationManager) 
       this.getSystemService(Context.NOTIFICATION_SERVICE); 

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
       new Intent(this, FbActivity.class), 0); 

     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()); 
    } 
} 

Я использую эту строку, чтобы отправить massege:

gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data); 

sender_id мой номер проекта на Google API консоли и идентификатор всегда одинаков (1).

ответ

1

gcm.send предназначен для отправки сообщений GCM из вашего приложения на ваш сервер (через GCM Cloud Connection Server). Ваш манифест выглядит хорошо, но для того, чтобы сделать gcm.send, вы должны иметь сервер, подключенный к GCM Cloud Connection Server. И поскольку вы пытаетесь отправить сообщение на сервер, вы ничего не увидите на своем устройстве (даже если сообщение было успешно отправлено на ваш сервер), и BroadcastReceiver не будет вызываться (если ваш сервер не отправит сообщение на Устройство).

+0

Hah, что я думал, это не могло быть так просто! Спасибо за то, что кажется, что у меня есть работа, спасибо! – Juvi

Смежные вопросы