Project: titanium
Issue TIMOB-18098: Android: JavaScript Service calling the notify method throws an error if the application is backed out
REPRODUCTION: Build the application using a master version of the Titanium SDK and run in the Genymotion emulator. Click the 'Add Notification' button. Back out of the application till it closes. Wait 30s for the notification to trigger. RESULTS: When the service tries to call the Android.NotificationManager.notify() method it throws an exception. {noformat} [WARN] : W/System.err java.lang.NullPointerException [WARN] : W/System.err android.app.Notification$Builder.applyStandardTemplate(Notification.java:1630) [WARN] : W/System.err android.app.Notification$Builder.makeContentView(Notification.java:1752) [WARN] : W/System.err android.app.Notification$Builder.buildUnstyled(Notification.java:1800) [WARN] : W/System.err android.app.Notification$Builder.build(Notification.java:1872) [WARN] : W/System.err android.support.v4.app.NotificationCompatKitKat$Builder.build(NotificationCompatKitKat.java:114) [WARN] : W/System.err android.support.v4.app.NotificationCompat$NotificationCompatImplKitKat.build(NotificationCompat.java:633) [WARN] : W/System.err android.support.v4.app.NotificationCompat$Builder.build(NotificationCompat.java:1503) [WARN] : W/System.err ti.modules.titanium.android.notificationmanager.NotificationProxy.buildNotification(NotificationProxy.java:315) [WARN] : W/System.err ti.modules.titanium.android.notificationmanager.NotificationManagerModule.notify(NotificationManagerModule.java:78) [WARN] : W/System.err org.appcelerator.kroll.runtime.v8.V8Runtime.nativeRunModule(Native Method) [WARN] : W/System.err org.appcelerator.kroll.runtime.v8.V8Runtime.doRunModule(V8Runtime.java:177) [WARN] : W/System.err org.appcelerator.kroll.KrollRuntime.handleMessage(KrollRuntime.java:299) [WARN] : W/System.err org.appcelerator.kroll.runtime.v8.V8Runtime.handleMessage(V8Runtime.java:203) [WARN] : W/System.err android.os.Handler.dispatchMessage(Handler.java:98) [WARN] : W/System.err android.os.Looper.loop(Looper.java:136) [WARN] : W/System.err org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:112) [WARN] : TiExceptionHandler (main) [5002,552687] Activity is null or already finishing, skipping dialog. [ERROR] : V8Exception Exception occurred at ExampleService.js:47 Uncaught Error Java Exception occurred {noformat} OTHER INFO: If you hit the home button instead to close the application, the notification works. If you remove the notify call from the service, the service runs OK. This works fine with 3.4.0.GA and 3.4.1.GA. *./Resources/app.js* {code} var win = Ti.UI.createWindow(); var btn = Ti.UI.createButton({ title : 'Add Notification' }); btn.addEventListener('click', function(e) { var intent = Ti.Android.createServiceIntent({ url : 'ExampleService.js' }); intent.putExtra('title' , 'Richard III'); intent.putExtra('message' , 'Now is the winter of our discontent...'); intent.putExtra('timestamp', new Date(new Date().getTime() + 30 * 1000)); intent.putExtra('interval', 5000); Ti.Android.startService(intent); Ti.App.Properties.setBool('bind', false); }); win.add(btn); win.open(); {code} *./Resources/ExampleService.js* {code} var service = Ti.Android.currentService; var serviceIntent = service.getIntent(); var timestamp = new Date(serviceIntent.getStringExtra('timestamp')); Ti.API.info(service.serviceInstanceId); if (!Ti.App.Properties.getBool('bind')) {	service.addEventListener("taskremoved", function(){	Ti.API.info("**************************** taskremoved fired");	});	service.addEventListener("pause", function(){	Ti.API.info("**************************** pause fired");	});	service.addEventListener("resume", function(){	Ti.API.info("**************************** resume fired");	});	Ti.App.Properties.setBool('bind', true); } if (new Date() > timestamp) { // Grab extra data sent with the intent var title = serviceIntent.getStringExtra('title'); var message = serviceIntent.getStringExtra('message'); // Create an intent that launches the application var intent = Ti.Android.createIntent({ action : Ti.Android.ACTION_MAIN, className: 'com.appcelerator.testapns.ActionnewsActivity', packageName: 'com.appcelerator.testapns', flags : Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP }); intent.addCategory(Ti.Android.CATEGORY_LAUNCHER); // Create notification var notification = Ti.Android.createNotification({ contentIntent : Ti.Android.createPendingIntent({intent : intent}), contentTitle : title, contentText : message }); // Send the notification Ti.Android.NotificationManager.notify(service.serviceInstanceId, notification); // Stop the service once the notification is sent Ti.Android.stopService(serviceIntent); } {code} *Replace android element with the following in tiapp.xml* {code} {code}
Story points: 8