<div class="markdown">
<p dir="auto">Erik, Sebastien,</p>

<p dir="auto">We’ve spent a significant part of the weekend looking into this issue and we have still not got to the bottom of it. </p>

<p dir="auto">We have built a very simple Ionic test app that works in isolation. We have used the default Ionic tabs at </p>

<p dir="auto"><a href="http://ionicframework.com/getting-started/">http://ionicframework.com/getting-started/</a></p>

<p dir="auto">We then add in our Javascript files as &lt;script&gt; entries in the index.html file (shown for example)</p>

<pre><code>    &lt;script src="js/polygon.js"&gt;&lt;/script&gt;
    &lt;script src="js/urls.js"&gt;&lt;/script&gt;
    &lt;script src="js/utilities.js"&gt;&lt;/script&gt;

    &lt;script src="js/moment.js"&gt;&lt;/script&gt;
    &lt;script src="js/perimeter.js"&gt;&lt;/script&gt;
    &lt;script src="js/leaflet.markercluster-src.js"&gt;&lt;/script&gt;
    &lt;script src="js/cycle.js"&gt;&lt;/script&gt;
    &lt;script src="js/clipper.js"&gt;&lt;/script&gt;
    &lt;script src="js/easy_button.js"&gt;&lt;/script&gt;
    &lt;script src="js/graham_scan.js"&gt;&lt;/script&gt;
    &lt;script src="js/sha.js"&gt;&lt;/script&gt;
    &lt;script src="js/r-tree.js"&gt;&lt;/script&gt;
    &lt;script src="js/angular-leaflet-directive.min.js"&gt;&lt;/script&gt;
    &lt;script src="pouchdb/L.TileLayer.PouchDBCached.js"&gt;&lt;/script&gt;
    &lt;script src="pouchdb/pouchdb-5.1.0.min.js"&gt;&lt;/script&gt;
</code></pre>

<p dir="auto">We do NOT call any functions within any of these files, indeed we make no reference to these files beyond including them in the index.html file. Our dummy html templates make no reference to them. We simply load the javascript files in. We get no errors from including the files. We have used the controllers and services as provided by the dummy sample Ionic app. </p>

<p dir="auto">If we then send foreground notifications, the notifications are always handled by the Aerogear notification handler.</p>

<p dir="auto">If we send notifications to the app when it is in the background, the notifications are always handled by the Aerogear notification handler.</p>

<p dir="auto">If we kill by swiping up the app, we get the notifications but when we click on the notification from the main IOS screen it is arbitrary as to whether the notification is handled by the Aerogear handler when the app starts up. Sometimes the handler is called, but once the handler is not called, the Aerogear notification handler never appears to be called again. If we reinstall the app it sometimes works again and sometimes it fails. </p>

<p dir="auto">We can reinstall the app and the Aerogear notification handler might be called or it might not. We just ran the app with 20 tests to the app, each time killing the app and then sending down a a notification and it started the Aerogear notification handler each time. We then reinstall the app again, no changes in the code and it simply doesn’t process any notification when the app is closed. The notification turns up, but no event is called to the Aerogear event handler. </p>

<p dir="auto">Given that we are getting arbitrary results from the same codebase, clearly something is remiss. Sometimes it works and sometimes it doesn’t. There is no logic that we can see. We’ve closed down Xcode after installing, we’ve started and stopped our sample app a few times to try and make sure its closed down, ew are sending exactly the same payload each time. </p>

<p dir="auto">We feel that there is some sort of timing or race condition, or if this was a piece of C code, that we are overwriting somewhere we shouldn’t be. It has that sort of feeling of a pointer going bad but we cannot see where the issue is at all. The fact that we get different results from doing the same install is very worrying but we can’t say whether its the Aerogear plugin, Ionic or our code. </p>

<p dir="auto">Thanks,</p>

<p dir="auto">Rob</p>

<p dir="auto">On 18 Dec 2015, at 17:26, Rob Willett wrote:</p>

<blockquote>
<p dir="auto">Erok,</p>

<p dir="auto">We’ve not forgotten the notification issue, we’re still working on it to try and get to the bottom of it. The problem for us is that its not so simple to cut code out and try and reduce the problem down. Our code is quite interlinked, for good or for worse.</p>

<p dir="auto">We’ve added some simple sound debugging to the Objective C source for the Aerogear plugin. This means that we can hear where things are when the plugin starts up from a cold start after receiving a notification. This works quite well.</p>

<p dir="auto">We initially added a beep to here</p>

<pre><code>- (void)notificationReceived {
 NSLog(@"Notification received");

 AudioServicesPlaySystemSound(1005);


 if (notificationMessage &amp;&amp; self.callbackId) {
</code></pre>

<p dir="auto">and this works whenever we get a notification in the foreground and when the app is in the background. We then started tracing backwards in the source code to see what called notificationReceived</p>

<p dir="auto">We can see it here</p>

<pre><code>- (void)register:(CDVInvokedUrlCommand *)command; {
 NSLog(@"register");
 self.callbackId = command.callbackId;

 AudioServicesPlaySystemSound(1007);

 isInline = NO;

 [self.commandDelegate runInBackground:^{
     NSMutableDictionary *options = [self parseOptions:command];
     [self saveConfig:options];

     // when running under iOS 8 we will use the new API for APNS registration
     #if __IPHONE_OS_VERSION_MAX_ALLOWED &gt;= 80000
         if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
             UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
             [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
             [[UIApplication sharedApplication] registerForRemoteNotifications];
         } else {
             [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
         }

     #else
         [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
     #endif

     CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
     [pluginResult setKeepCallback:@YES];
     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
 }];

 if (notificationMessage)            // if there is a pending startup notification
 {
     AudioServicesPlaySystemSound(1024);

     [self notificationReceived];    // go ahead and process it
 }
}
</code></pre>

<p dir="auto">So we add some more beeps in to track down whats going on. We add AudioServicesPlaySystemSound(1007) at the beginning of the function and add AudioServicesPlaySystemSound(1024) just before we call the notificationReceived method.</p>

<p dir="auto">We get the first sounds on startup but do NOT get the second set of sounds. It looks like notificationMessage  is not being set at application startup.</p>

<p dir="auto">Our problem now is that we are not Objective-C developers so we are struggling to debug much further. So trying to understand how notificationMessage is set and defined as its declared as @synthesis is unclear. We’ll start reading and learning quickly but as this is new for us, so we will be slow.</p>

<p dir="auto">Any suggestions welcomed,</p>

<p dir="auto">Rob</p>

<p dir="auto">On 16 Dec 2015, at 16:54, Erik Jan de Wit wrote:</p>

<blockquote>
<p dir="auto">Hi Rob,</p>

<p dir="auto">What you could try is to debug this in xcode, when the notification is<br>
touched it should launch the app and that will in turn call the JS<br>
callback. Would be good to see if this code is called 100% of the time.</p>

<p dir="auto">You can put a breakpoint here:<br>
<a href="https://github.com/aerogear/aerogear-cordova-push/blob/master/src/ios/AppDelegate%2Bnotification.m#L56">https://github.com/aerogear/aerogear-cordova-push/blob/master/src/ios/AppDelegate%2Bnotification.m#L56</a></p>

<p dir="auto">That code will execute on cold start and here:<br>
<a href="https://github.com/aerogear/aerogear-cordova-push/blob/master/src/ios/AGPushPlugin.m#L102">https://github.com/aerogear/aerogear-cordova-push/blob/master/src/ios/AGPushPlugin.m#L102</a></p>

<p dir="auto">That is where the onNotification callback gets invoked.</p>

<p dir="auto">Hope this helps,</p>

<p dir="auto">On Wed, Dec 16, 2015 at 5:14 PM, Rob Willett &lt;<a href="mailto:rob.aerogear@robertwillett.com">rob.aerogear@robertwillett.com</a></p>

<blockquote>
<p dir="auto">wrote:</p>

<p dir="auto">Sebastien</p>

<p dir="auto">Yes, we do it at that point, $ionicPlatform.ready. I include the whole of<br>
that area of code for reference but we’re not expecting you to debug it for<br>
us. Merely to demonstrate its there.</p>

<p dir="auto">At the moment all we want the code to do is put an alert up.</p>

<p dir="auto">.run(function($ionicPlatform , CordovaService) {<br>
$ionicPlatform.ready(function() {<br>
if (window.StatusBar) {<br>
  // org.apache.cordova.statusbar required<br>
  StatusBar.styleDefault();<br>
}</p>

<p dir="auto">ConsoleLog('&lt;&lt;&lt; Cordova ready &gt;&gt;&gt;');</p>

<p dir="auto">/* This seems to remove an annoying page flicker on iOS when the keyboard is displayed */<br>
cordova.plugins.Keyboard.disableScroll(true);</p>

<p dir="auto">isDeviceReady = true;</p>

<p dir="auto">var uuid = "D26FBAF1-2EF2-4614-875F-4497EC4212D5/JFL1-0/ios_app";</p>

<p dir="auto">var aeroGearPushConfig = {<br>
  pushServerURL: "<a href="https://push-jambuster.rhcloud.com/ag-push/">https://push-jambuster.rhcloud.com/ag-push/</a>",<br>
  ios: {<br>
      variantID: “XXXXX”,<br>
      variantSecret: “YYYYYYY”<br>
  } ,<br>
  // sendMetricInfo: true,<br>
  alias: uuid<br>
};</p>

<p dir="auto">push.register(function (event) {<br>
  alert("EVENT = " + JSON.stringify(event));</p>

<p dir="auto">} ,  function () {<br>
  if (1)<br>
  {<br>
      // alert("AeroGearSuccessHandler: OK " + JSON.stringify(uuid));<br>
      // ConsoleLog("UUID = " + uuid);<br>
  }<br>
} , function () {<br>
} , aeroGearPushConfig);</p>

<p dir="auto">We were still loading up another 18,000 lines of code so we need to start<br>
pruning that back until we have something that works.</p>

<p dir="auto">Rob</p>

<p dir="auto">On 16 Dec 2015, at 16:04, Sebastien Blanc wrote:</p>

<p dir="auto">In the ionic app when do you do the registration of UPS ? on the<br>
platformReady event ?</p>

<p dir="auto">On Wed, Dec 16, 2015 at 4:54 PM, Rob Willett &lt;<br>
<a href="mailto:rob.aerogear@robertwillett.com">rob.aerogear@robertwillett.com</a></p>

<p dir="auto">wrote:</p>

<p dir="auto">Erik,</p>

<p dir="auto">We have built the simplest possible app we can that uses the Aerogear<br>
push plugin but using the ionic tabs starter kit.</p>

<p dir="auto"><a href="http://ionicframework.com/getting-started/">http://ionicframework.com/getting-started/</a></p>

<p dir="auto">We have taken the code directly from the Cordova simple app that we have<br>
got working and put it into the Ionic app.</p>

<p dir="auto">if we follow your tests as below:</p>

<p dir="auto">1.</p>

<p dir="auto">IOS App in foreground, we send a simple push notification from the<br>
Aerogear console - Works OK, We can see the event. Good<br>
2.</p>

<p dir="auto">IOS App in background, we send a simple push notification from the<br>
Aerogear console - Works OK, We can see the event. Good<br>
3.</p>

<p dir="auto">IOS App killed, we send a simple push notification from the Aerogear</p>

<p dir="auto">console and some of the time when we click on the notification in the<br>
notification drawer we do NOT get the notification handler called. Not<br>
so good</p>

<p dir="auto">However it does appear to work most of the time with our minimal Ionic<br>
app, but some of the time it fails. We had a run of 1 in 2 failures when<br>
we click on the notification. Now we have just done 20 runs in a row,<br>
each time killing the app after receiving the notification and not a<br>
single failure. Nothing changed.</p>

<p dir="auto">We cannot find any obvious reason for this so we are still<br>
investigating.</p>

<p dir="auto">We have reconfigured our main app to work the same way as the minimal<br>
app but we are still getting the same issues as before, we can see the<br>
notification in the drawer but clicking on it does NOT call the same<br>
handler with the same code as the minimal app. We get zero calls to the<br>
notification event handler.</p>

<p dir="auto">We are wondering if there is a timing issue somewhere in our code, but<br>
we can’t see it. We also wondered if the size of the code we are<br>
loading is the cause of a timing issue as well.</p>

<p dir="auto">Is there any way of adding more debugging into the Aerogear push plugin<br>
to see if we can track things down that way?</p>

<p dir="auto">Its very frustrating, but thanks for your help to date. It does look<br>
like its an interaction with our code, Ionic and the Aerogear plugin. My<br>
money is on our code though. We’ll now start pulling working code out<br>
of our app until we get back to the minimal app. Only 18,604 lines to go<br>
:)</p>

<p dir="auto">Rob</p>

<p dir="auto">On 15 Dec 2015, at 10:36, Erik Jan de Wit wrote:</p>

<p dir="auto">Hi Rob,</p>

<p dir="auto">That would be a bug, although I can not reproduce it, to test it this<br>
is<br>
what I've done to test it:<br>
$ &gt; cordova create push-test &lt;my bundle id&gt;<br>
$ &gt; cordova platform add ios<br>
$ &gt; cordova plugin add aerogear-cordova-push</p>

<p dir="auto">copy paste your js code into www/js/index.js onDeviceReady, changed<br>
pushServerUrl and variant info and changed quotes to " (this is your<br>
email<br>
client no doubt) and changed : into ; after console.log("Success")</p>

<p dir="auto">Attach Safari debugger and send a message when the app is in the<br>
foreground<br>
and get in the console:</p>

<p dir="auto">HandleAeroGearNotification: event =&gt;</p>

<p dir="auto">{"alert":"test","foreground":true,"coldstart":false,"sound":"default","badge":-1,"payload":{}}</p>

<p dir="auto">I press the home button and send the app to the background send<br>
another<br>
message and 'touch' the message to launch the app:</p>

<p dir="auto">HandleAeroGearNotification: event =&gt;</p>

<p dir="auto">{"alert":"background","foreground":false,"coldstart":false,"sound":"default","badge":-1,"payload":{}}</p>

<p dir="auto">The I kill the app by pressing home twice and swiping over the app to<br>
remove it send another message and 'touch' it to launch the app. This<br>
kills<br>
my safari debugger so no way to see the console log, but in this case<br>
coldstart should be true. To test this better changed the code to<br>
alert<br>
instead of console:</p>

<p dir="auto">function HandleAeroGearNotification(event) {<br>
alert("HandleAeroGearNotification: event =&gt; " + event.coldstart);</p>

<p dir="auto">// Stuff cut for clarity<br>
}</p>

<p dir="auto">I send another notification 'touch' it to launch the app and see the<br>
alert<br>
box display the text:</p>

<p dir="auto">HandleAeroGearNotification: event =&gt; true</p>

<p dir="auto">Hope this helps</p>

<p dir="auto">On Mon, Dec 14, 2015 at 10:20 PM, Rob Willett &lt;<br>
<a href="mailto:rob.aerogear@robertwillett.com">rob.aerogear@robertwillett.com</a>&gt; wrote:</p>

<p dir="auto">Hi,</p>

<p dir="auto">We think we have found a possible bug in the Aerogear Cordova 2.0.4<br>
push<br>
plugin specifically on the iOS side.<br>
Summary</p>

<p dir="auto">We have two versions of our app, an Android and an IOS version. Both<br>
use<br>
the latest Cordova push plugin 2.0.4. They also both have the latest<br>
Cordova platforms, android 4.1.1, ios 3.9.2. Both the iOS and Android<br>
versions are compiled at the same time. We are running cordova 5.3.3<br>
with<br>
Ionic 1.7.8 (?).</p>

<p dir="auto">1.</p>

<p dir="auto">We make sure that both apps are NOT started up on each device. We<br>
also<br>
check they are NOT in the background.<br>
2.</p>

<p dir="auto">We send the same simple notification to each device. This<br>
notification<br>
config is as below, we have anonymised the variants and alias in this<br>
JSON<br>
structure, though we can report that the UPS server sends the data<br>
correctly. We use the additionalData flag to provide the information<br>
necessary to decide which notification has been clicked in the<br>
notification<br>
drawer.</p>

<p dir="auto">'variants' =&gt; [‘variant1’,’variant2’ ],<br>
'message' =&gt; {<br>
'additionalData' =&gt; { 'Disruption_Id' =&gt; '107546',<br>
'EpochTime' =&gt; '1450125268'<br>
},<br>
'alert' =&gt; 'Cannon Street (EC4N) (All Directions) at the junction of<br>
King William Street - To facilitate a heavy lift in Cannon Street,<br>
Cannon Street will be closed. Traffic is slow moving on diversion.'<br>
},<br>
'alias' =&gt; [ ‘alias1’ ],<br>
'ttl' =&gt; 600<br>
};</p>

<p dir="auto">1.</p>

<p dir="auto">Both devices show the message, the Android device stacks the message<br>
and the iOS device display an individual message. This looks correct.<br>
2.</p>

<p dir="auto">Clicking on the Android stacked message starts up the app and the<br>
Javascript notification handler we have defined,<br>
HandleAeroGearNotification<br>
is called</p>

<p dir="auto">HandleAeroGearNotification: event =&gt; {"alert":"Cannon Street (EC4N)<br>
(All Directions) at the junction of King William Street - To<br>
facilitate a heavy lift in Cannon Street, Cannon Street will be<br>
closed. Traffic is slow moving on</p>

<p dir="auto">diversion.","coldstart":true,"foreground":true,"payload":{"alert":"Cannon</p>

<p dir="auto">Street (EC4N) (All Directions) at the junction of King William Street<br>
- To facilitate a heavy lift in Cannon Street, Cannon Street will be<br>
closed. Traffic is slow moving on diversion.","badge":"-1"}}</p>

<p dir="auto">1.</p>

<p dir="auto">Clicking on the notification in the notification drawer on the iOS<br>
device also starts our app up correctly but the notification handler,<br>
HandleAeroGearNotification(), is NOT called. The app starts up as<br>
normal as<br>
if the notification had not been clicked. We would expect the<br>
notification<br>
handler to be called in iOS as it is in Android.<br>
2.</p>

<p dir="auto">All notifications are cleared on both Android and iOS correctly when<br>
the app is started up.<br>
3.</p>

<p dir="auto">We define HandleAeroGearNotification as</p>

<p dir="auto">var aeroGearPushConfig = {<br>
pushServerURL: "<a href="https://push-jambuster.rhcloud.com/ag-push/">https://push-jambuster.rhcloud.com/ag-push/</a>",<br>
ios: {<br>
variantID: “variantid_obscured”,<br>
variantSecret: “variant_secret_obscured”<br>
} ,<br>
android: {<br>
senderID: "variantid_obscured" ,<br>
variantID: "variant_id_obscured" ,<br>
variantSecret: "variant_secret_obscured"<br>
} ,<br>
sendMetricInfo: true,<br>
alias: alias1<br>
};</p>

<p dir="auto">function HandleAeroGearNotification(event) {<br>
console.log(“HandleAeroGearNotification: event =&gt; “ +<br>
JSON.stringify(event));</p>

<p dir="auto">// Stuff cut for clarity<br>
}</p>

<p dir="auto">// Slightly simplified registration event.<br>
push.register(HandleAeroGearNotification , function () {<br>
console.log(“Success”):<br>
} , function () {<br>
console.log(“Failure”);<br>
} , aeroGearPushConfig);</p>

<p dir="auto">We cannot see any reference to this issue in the JIRA database and<br>
wondered if it is a bug or not.</p>

<p dir="auto">If its a bug we are happy to raise it accordingly.</p>

<p dir="auto">Please let us know,</p>

<p dir="auto">Thanks</p>

<h2>Rob</h2>

<p dir="auto">Aerogear-users mailing list<br>
<a href="mailto:Aerogear-users@lists.jboss.org">Aerogear-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/aerogear-users">https://lists.jboss.org/mailman/listinfo/aerogear-users</a></p>

<p dir="auto">--<br>
Cheers,</p>

<h2>Erik Jan</h2>

<p dir="auto">Aerogear-users mailing list<br>
<a href="mailto:Aerogear-users@lists.jboss.org">Aerogear-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/aerogear-users">https://lists.jboss.org/mailman/listinfo/aerogear-users</a></p>

<hr>

<p dir="auto">Aerogear-users mailing list<br>
<a href="mailto:Aerogear-users@lists.jboss.org">Aerogear-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/aerogear-users">https://lists.jboss.org/mailman/listinfo/aerogear-users</a></p>

<hr>

<p dir="auto">Aerogear-users mailing list<br>
<a href="mailto:Aerogear-users@lists.jboss.org">Aerogear-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/aerogear-users">https://lists.jboss.org/mailman/listinfo/aerogear-users</a></p>

<hr>

<p dir="auto">Aerogear-users mailing list<br>
<a href="mailto:Aerogear-users@lists.jboss.org">Aerogear-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/aerogear-users">https://lists.jboss.org/mailman/listinfo/aerogear-users</a></p>
</blockquote>

<p dir="auto">-- <br>
Cheers,<br>
Erik Jan</p>

<hr>

<p dir="auto">Aerogear-users mailing list<br>
<a href="mailto:Aerogear-users@lists.jboss.org">Aerogear-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/aerogear-users">https://lists.jboss.org/mailman/listinfo/aerogear-users</a></p>
</blockquote>

<hr>

<p dir="auto">Aerogear-users mailing list<br>
<a href="mailto:Aerogear-users@lists.jboss.org">Aerogear-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/aerogear-users">https://lists.jboss.org/mailman/listinfo/aerogear-users</a></p>
</blockquote>

</div>