title: Android: How to use Parse as an alternative GCM push notification provider?
body: <p><strong>SEE EDIT#2 at the end of the question (Google updated the way push is implemented so it became easier to handle gcm and parse together)</strong></p>
  
  <p>I already use GCM inside a application and I would like to add parse as an alternative. This is what I have now (all the permissions are correctly declared):</p>
  
  <pre><code>&lt;service 
     android:name="com.mypackagename.GCMIntentService" 
     android:enabled="true" /&gt;
  &lt;receiver 
     android:name="com.google.android.gcm.GCMBroadcastReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND" &gt;
     &lt;intent-filter&gt;
         &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
         &lt;action android:name="com.google.android.c2dm.intent.REGISTRATION" /&gt;
         &lt;category android:name="com.mypackagename.GCMIntentService" /&gt;
     &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  </code></pre>
  
  <p>The 'GCMIntentService' (inherits from 'GCMBaseIntentService') class handles the server registration and receiving of messages - it works fine and all the push messages are received. The server sometimes sends custom data so I handle the messages myself and create the notifications programmatically (the intent used when the notification is clicked has some important extras sent from the server).</p>
  
  <p>I would like to somehow make parse behave in the same way in order to be able to send channel pushes from the parse website and create my own notifications, but everything I tried failed (following the android push tutorial isn't really working for my problem). Is there anyone who tried a similar thing? I'm kind of out of ideas after spending a lot of time tweaking the push guides/tutorials - sometimes I don't receive any notifications; sometimes both parse and my receiver are called and I get double notifications. I also tried to register using parse REST apis and handle everything myself but found out it isn't possible on Android.</p>
  
  <p>So, how could I handle both parse pushes and the traditional gcm pushes (using my server) in such a way that I have access to both notifications and I can build them from scratch (create my own pending notifications with the required extras)?</p>
  
  <p><strong>EDIT#1:</strong></p>
  
  <p>The first thing I tried was to use the parse service and have a single broadcast receiver to handle the GCM messages:</p>
  
  <p>AndroidMaifest.xml:</p>
  
  <pre><code>&lt;service 
      android:name="com.mypackagename.GCMIntentService" 
      android:enabled="true" /&gt;
  &lt;service android:name="com.parse.PushService"
      android:enabled="true"/&gt;
  &lt;receiver 
      android:name="com.google.android.gcm.GCMBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" &gt;
          &lt;intent-filter&gt;
          &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
          &lt;action android:name="com.google.android.c2dm.intent.REGISTRATION" /&gt;
          &lt;category android:name="com.mypackagename.GCMIntentService" /&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  </code></pre>
  
  <p>And the parse library requires the following initializations:</p>
  
  <pre><code>Parse.initialize(context, appId, apiKey);
  PushService.setDefaultPushCallback(context, MainActivity.class);
  // I'm subscribing to channel push because I send channel pushes from the
  // parse console
  PushService.subscribe(context, MDConstants.PARSE_PUSH_CHANNEL, MainActivity.class);
  ParseInstallation.getCurrentInstallation().saveInBackground();
  </code></pre>
  
  <p>The problem is that I receive the notifications from my other provider but I don't receive anything from parse (all the permissions are declared) and I get the following error from the parse library (when receiving the error the parse registration is not properly done - I can't see my device in the parse console):</p>
  
  <pre><code>E/com.parse.ManifestInfo(11677): Cannot use GCM for push because the app manifest is missing some required declarations. Please make sure that these permissions are declared as children of the root &lt;manifest&gt; element:
  E/com.parse.ManifestInfo(11677): 
  E/com.parse.ManifestInfo(11677): &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  E/com.parse.ManifestInfo(11677): &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  E/com.parse.ManifestInfo(11677): &lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
  E/com.parse.ManifestInfo(11677): &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  E/com.parse.ManifestInfo(11677): &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;
  E/com.parse.ManifestInfo(11677): &lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt;
  E/com.parse.ManifestInfo(11677): &lt;permission android:name="com.mypackagename.permission.C2D_MESSAGE" android:protectionLevel="signature" /&gt;
  E/com.parse.ManifestInfo(11677): &lt;uses-permission android:name="com.mypackagename.permission.C2D_MESSAGE" /&gt;
  E/com.parse.ManifestInfo(11677): 
  E/com.parse.ManifestInfo(11677): Also, please make sure that these services and broadcast receivers are declared as children of the &lt;application&gt; element:
  E/com.parse.ManifestInfo(11677): 
  E/com.parse.ManifestInfo(11677): &lt;service android:name="com.parse.PushService" /&gt;
  E/com.parse.ManifestInfo(11677): &lt;receiver android:name="com.parse.GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND"&gt;
  E/com.parse.ManifestInfo(11677):   &lt;intent-filter&gt;
  E/com.parse.ManifestInfo(11677):     &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
  E/com.parse.ManifestInfo(11677):     &lt;action android:name="com.google.android.c2dm.intent.REGISTRATION" /&gt;
  E/com.parse.ManifestInfo(11677):     &lt;category android:name="com.mypackagename" /&gt;
  E/com.parse.ManifestInfo(11677):   &lt;/intent-filter&gt;
  E/com.parse.ManifestInfo(11677): &lt;/receiver&gt;
  E/com.parse.PushService(11677): Tried to use push, but this app is not configured for push due to: Push is not configured for this app because the app manifest is missing required declarations. Please add the following declarations to your app manifest to support either GCM or PPNS for push (or both). To enable GCM support, please make sure that these permissions are declared as children of the root &lt;manifest&gt; element:
  E/com.parse.PushService(11677): 
  E/com.parse.PushService(11677): &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  E/com.parse.PushService(11677): &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  E/com.parse.PushService(11677): &lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
  E/com.parse.PushService(11677): &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  E/com.parse.PushService(11677): &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;
  E/com.parse.PushService(11677): &lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt;
  E/com.parse.PushService(11677): &lt;permission android:name="com.mypackagename.permission.C2D_MESSAGE" android:protectionLevel="signature" /&gt;
  E/com.parse.PushService(11677): &lt;uses-permission android:name="com.mypackagename.permission.C2D_MESSAGE" /&gt;
  E/com.parse.PushService(11677): 
  E/com.parse.PushService(11677): Also, please make sure that these services and broadcast receivers are declared as children of the &lt;application&gt; element:
  E/com.parse.PushService(11677): 
  E/com.parse.PushService(11677): &lt;service android:name="com.parse.PushService" /&gt;
  E/com.parse.PushService(11677): &lt;receiver android:name="com.parse.GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND"&gt;
  E/com.parse.PushService(11677):   &lt;intent-filter&gt;
  E/com.parse.PushService(11677):     &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
  E/com.parse.PushService(11677):     &lt;action android:name="com.google.android.c2dm.intent.REGISTRATION" /&gt;
  E/com.parse.PushService(11677):     &lt;category android:name="com.mypackagename" /&gt;
  E/com.parse.PushService(11677):   &lt;/intent-filter&gt;
  E/com.parse.PushService(11677): &lt;/receiver&gt;
  E/com.parse.PushService(11677): To enable PPNS support, please make sure that these permissions are declared as children of the root &lt;manifest&gt; element:
  E/com.parse.PushService(11677): 
  E/com.parse.PushService(11677): &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  E/com.parse.PushService(11677): &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  E/com.parse.PushService(11677): &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
  E/com.parse.PushService(11677): &lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
  E/com.parse.PushService(11677): &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  E/com.parse.PushService(11677): 
  E/com.parse.PushService(11677): Also, please make sure that these services and broadcast receivers are declared as children of the &lt;application&gt; element:
  E/com.parse.PushService(11677): 
  E/com.parse.PushService(11677): &lt;service android:name="com.parse.PushService" /&gt;
  E/com.parse.PushService(11677): &lt;receiver android:name="com.parse.ParseBroadcastReceiver"&gt;
  E/com.parse.PushService(11677):   &lt;intent-filter&gt;
  E/com.parse.PushService(11677):     &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
  E/com.parse.PushService(11677):     &lt;action android:name="android.intent.action.USER_PRESENT" /&gt;
  E/com.parse.PushService(11677):   &lt;/intent-filter&gt;
  E/com.parse.PushService(11677): &lt;/receiver&gt;
  </code></pre>
  
  <p><strong>EDIT#2:</strong></p>
  
  <p>I updated the way gcm push was handled based on the <a href="https://developers.google.com/cloud-messaging/android/client" rel="nofollow">google push notification developer guide</a>. While implementing the class that extends 'GcmListenerService', you can now easily check if the 'from' arguments is the same as your google project id used to register for push.</p>
  
  <pre><code>public class MyGcmListenerService extends GcmListenerService {
      @Override
      public void onMessageReceived(String from, Bundle data) {
          // only handle gcm messages that come from the same project id used to register
          if (from.equals("Your google project id)) {
              // handle the gcm push
          }
      }
  }
  </code></pre>
  
  <p>Also, parse updated their libraries (I'm using '1.9.4' right now) and you can subclass the 'ParsePushBroadcastReceiver' to handle the notifications as you'd like. See the guide <a href="https://parse.com/tutorials/android-push-notifications" rel="nofollow">here</a> for a basic implementation.</p>
  
  <p>Parse initialization in the 'onCreate' method of your 'Application' class:</p>
  
  <pre><code>Parse.initialize(this, "your parse app id", "your parse client key");
  
  // subscribing to a channel
  ParsePush.subscribeInBackground("your channel name", new SaveCallback() {
      @Override
      public void done(ParseException e) {
          if (e == null) {
              Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
          } else {
              Log.e("com.parse.push", "failed to subscribe for push");
          }
      }
  });
  </code></pre>
  
  <p>The broadcast receiver implementation:</p>
  
  <pre><code>public class MyParsePushBroadcastReceiver extends ParsePushBroadcastReceiver {
      @Override
      protected void onPushReceive(Context context, Intent intent) {
          // handle the parse push notification
      }
  }
  </code></pre>
  
  <p>The manifest declaration for both parse and gcm:</p>
  
  <pre><code>...
  
  &lt;!-- GCM listener service --&gt;
  &lt;service
      android:name=".MyGcmListenerService"
      android:exported="false" &gt;
      &lt;intent-filter&gt;
          &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
      &lt;/intent-filter&gt;
  &lt;/service&gt;
  
  ...
  
  &lt;!-- Parse broadcast receiver --&gt;
  &lt;receiver android:name=".MyParsePushBroadcastReceiver" android:exported="false"&gt;
      &lt;intent-filter&gt;
          &lt;action android:name="com.parse.push.intent.RECEIVE" /&gt;
          &lt;action android:name="com.parse.push.intent.DELETE" /&gt;
          &lt;action android:name="com.parse.push.intent.OPEN" /&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  
  ...
  </code></pre>
  
  <p>I only added the service and the receiver, but you need to make sure you follow the GCM guide and the Parse push guides to have a full implementation (for example, google also added a way to handle the token refresh - a sample containing full code samples can be found <a href="https://github.com/googlesamples/google-services/tree/master/android/gcm/app/src/main/java/gcm/play/android/samples/com/gcmquickstart" rel="nofollow">here</a>).</p>