title: Worklight: error Push Notification on Android
body: <p>Please help me! I've tried for a week to fix my push notification demo but it still doesn't work! Device could not recieve messages after I quit my application. </p>
  
  <p>This is the android element from application-descriptor.xml file (with Browser Key):</p>
  
  <pre><code> &lt;android version="1.0" securityTest="mobile-securityTest"&gt;
          &lt;worklightSettings include="false"/&gt;
          &lt;pushSender key="AIzaSyBmDTJw8IQAA1MmTF9MkSQUQXYxoFBKKtk" senderId="430560385547"/&gt;
          &lt;security&gt;
              &lt;encryptWebResources enabled="false"/&gt;
              &lt;testWebResourcesChecksum enabled="false" ignoreFileExtensions="png, jpg, jpeg, gif, mp4, mp3"/&gt;
              &lt;publicSigningKey&gt;PushNotificationMyAppAndroid&lt;/publicSigningKey&gt;
              &lt;packageName&gt;com.MyApp&lt;/packageName&gt;
          &lt;/security&gt;
          &lt;compressWebResources enabled="true"/&gt;
      &lt;/android&gt;
  </code></pre>
  
  <p>This is main.js file:</p>
  
  <pre><code>$('#isSupportbtn').bind("click", isSupport);
  $('#isSubscibedbtn').bind("click",isSubscribed);
  $('#subscribebtn').bind("click",doSubscribe);
  $('#unSubscribebtn').bind("click",doUnSubscribe);
  
  function isSupport(){
      var isSupport = false;
      if(WL.Client.Push){
          isSupport = WL.Client.Push.isPushSupported();
      }
      alert(isSupport);
  }
  function isSubscribed(){
      var isSubscribed = false;
      if(WL.Client.Push){
          isSubscribed = WL.Client.Push.isSubscribed('myPush');
      }
      alert(isSubscribed);
  }
  
  if(WL.Client.Push){
      WL.Client.Push.onReadyToSubscribe = function(){
          $('#subscribebtn').removeAttr('disabled');
          $('#unSubscribebtn').removeAttr('disabled');
          WL.Client.Push.registerEventSourceCallback(
              "myPush",
              "AdapterAuth",
              "pushEventSource",
              pushNotificationReceived
          );
      };
  }
  function doSubscribe(){
      WL.Client.Push.subscribe("myPush",{
          onSuccess:function(){
              alert("Success");
          },
          onFailure:function(){
              alert("Fail");
          }
      });
  }
  function doUnSubscribe(){
      WL.Client.Push.unsubscribe("myPush",{
          onSuccess:function(){
              alert("Success");
          },
          onFailure:function(){
              alert("Fail");
          }
      });
  }
  function pushNotificationReceived(props, payloads){
      alert("pushNotificationReceived invoked");
      alert("props :: " + JSON.stringify(props));
      alert("payload :: " + JSON.stringify(payload));
  }
  </code></pre>
  
  <p>This is AdapterAuth-impl.js file:</p>
  
  <pre><code>function getUser() {
      return {Data:[{username:'hcv',password:"123456",bagde:'3',message:'hi, Jane'},{username:'vi',password:'123',bagde:'3',message:'hi, Luffy'}]};
  }
  function onAuthRequired(headers,errorMessage){
      errorMessage = errorMessage ? errorMessage:null;
      return{
          authRequired:true,
          errorMessage:errorMessage
      };
  }
  function submitAuth(username,password) {
      var response = {Data:[{username:'hcv',password:"123456",bagde:'3',message:'hi, Jane'},{username:'vi',password:'123',bagde:'3',message:'hi, Luffy'}]};
      for (var i=0;i&lt;response.Data.length;i++){
          if(username === response.Data[i].username &amp;&amp; password === response.Data[i].password){
              var Identity={
                  userId: username,
                  displayName: username,
                  attributes:{foo:"bar"}
              };
              WL.Server.setActiveUser("AuthLoginRealm", Identity);
              return{
                  authRequired: false
              };
          }
      }
      return onAuthRequired(null,"Invalid login credentials");
  
  }
  function onLogout(){
      WL.Server.setActiveUser("AuthLoginRealm", null);
      WL.Logger.debug("Logged out");
  }
  WL.Server.createEventSource({
      name:"pushEventSource",
      poll:{
          interval:60,
          onPoll:"submitNotifications"
      },
      securityTest:"mobile-securityTest",
  
  });
  function submitNotifications() {
      var response = {Data:[{username:'hcv',password:"123456",bagde:"3",message:"hi,Jane"},{username:"vi",password:"123",bagde:"1",message:"hi, Luffy"}]};
      var messages = response.Data;
      for(var i=0; i &lt; messages.length;i++){
          var userId = WL.Server.getActiveUser("AuthLoginRealm");
          if(messages[i].username === userId){
          var userSubscription = WL.Server.getUserNotificationSubscription("AdapterAuth.pushEventSource", messages[i].username);
  
          if(userSubscription === null){
              return {result:"no user was found:"+messages[i].username};
          }
          var notification = WL.Server.createDefaultNotification(messages[i].message, messages[i].bagde,{foo:'bar'});
          WL.Logger.debug("submitNotification &gt;&gt; userId :: " + messages[i].username + ", text :: " + messages[i].message);
          WL.Server.notifyAllDevices(userSubscription, notification);
          WL.Logger.error("UserId"+messages[i].username+"not found");
          }
      }
  }
  </code></pre>
  
  <p>This is error:</p>
  
  <pre><code>06-24 09:27:11.927: E/ActivityThread(3096): Activity com.MyApp.MyApp has leaked IntentReceiver com.google.android.gcm.GCMBroadcastReceiver@41c85190 that was originally registered here. Are you missing a call to unregisterReceiver()?
  06-24 09:27:11.927: E/ActivityThread(3096): android.app.IntentReceiverLeaked: Activity com.MyApp.MyApp has leaked IntentReceiver com.google.android.gcm.GCMBroadcastReceiver@41c85190 that was originally registered here. Are you missing a call to unregisterReceiver()?
  06-24 09:27:11.927: E/ActivityThread(3096):     at android.app.LoadedApk$ReceiverDispatcher.&lt;init&gt;(LoadedApk.java:763)
  06-24 09:27:11.927: E/ActivityThread(3096):     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:567)
  06-24 09:27:11.927: E/ActivityThread(3096):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1055)
  06-24 09:27:11.927: E/ActivityThread(3096):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1042)
  06-24 09:27:11.927: E/ActivityThread(3096):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:348)
  06-24 09:27:11.927: E/ActivityThread(3096):     at com.google.android.gcm.GCMRegistrar.setRetryBroadcastReceiver(GCMRegistrar.java:293)
  06-24 09:27:11.927: E/ActivityThread(3096):     at com.google.android.gcm.GCMRegistrar.register(GCMRegistrar.java:215)
  06-24 09:27:11.927: E/ActivityThread(3096):     at com.worklight.androidgap.plugin.Push.subscribe(Push.java:331)
  06-24 09:27:11.927: E/ActivityThread(3096):     at com.worklight.androidgap.plugin.Push.access$400(Push.java:45)
  06-24 09:27:11.927: E/ActivityThread(3096):     at com.worklight.androidgap.plugin.Push$2.run(Push.java:233)
  06-24 09:27:11.927: E/ActivityThread(3096):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
  06-24 09:27:11.927: E/ActivityThread(3096):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
  06-24 09:27:11.927: E/ActivityThread(3096):     at java.lang.Thread.run(Thread.java:856)
  </code></pre>