Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-18098

Android: JavaScript Service calling the notify method throws an error if the application is backed out

    Details

    • Type: Bug
    • Status: Closed
    • Priority: High
    • Resolution: Fixed
    • Affects Version/s: Release 3.5.0
    • Fix Version/s: Release 5.0.0
    • Component/s: Android
    • Labels:
      None
    • Environment:

      Mac OS X 10.9.5
      Appcelerator Studio, build: 3.4.1.201410281743
      Ti SDK 3.5.0.v20141118102515, 3.6.0.v20141125154117, 3.4.1.GA, 3.4.0.GA
      CLI 3.5.0-dev
      Genymotion Google Nexus 7 - 4.4.4 - API 19 - 800x1280
      Android 5.0/API 21

    • Story Points:
      8
    • Sprint:
      2015 Sprint 15 SDK

      Description

      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.

      [WARN] :   W/System.err java.lang.NullPointerException
      [WARN] :   W/System.err 	at android.app.Notification$Builder.applyStandardTemplate(Notification.java:1630)
      [WARN] :   W/System.err 	at android.app.Notification$Builder.makeContentView(Notification.java:1752)
      [WARN] :   W/System.err 	at android.app.Notification$Builder.buildUnstyled(Notification.java:1800)
      [WARN] :   W/System.err 	at android.app.Notification$Builder.build(Notification.java:1872)
      [WARN] :   W/System.err 	at android.support.v4.app.NotificationCompatKitKat$Builder.build(NotificationCompatKitKat.java:114)
      [WARN] :   W/System.err 	at android.support.v4.app.NotificationCompat$NotificationCompatImplKitKat.build(NotificationCompat.java:633)
      [WARN] :   W/System.err 	at android.support.v4.app.NotificationCompat$Builder.build(NotificationCompat.java:1503)
      [WARN] :   W/System.err 	at ti.modules.titanium.android.notificationmanager.NotificationProxy.buildNotification(NotificationProxy.java:315)
      [WARN] :   W/System.err 	at ti.modules.titanium.android.notificationmanager.NotificationManagerModule.notify(NotificationManagerModule.java:78)
      [WARN] :   W/System.err 	at org.appcelerator.kroll.runtime.v8.V8Runtime.nativeRunModule(Native Method)
      [WARN] :   W/System.err 	at org.appcelerator.kroll.runtime.v8.V8Runtime.doRunModule(V8Runtime.java:177)
      [WARN] :   W/System.err 	at org.appcelerator.kroll.KrollRuntime.handleMessage(KrollRuntime.java:299)
      [WARN] :   W/System.err 	at org.appcelerator.kroll.runtime.v8.V8Runtime.handleMessage(V8Runtime.java:203)
      [WARN] :   W/System.err 	at android.os.Handler.dispatchMessage(Handler.java:98)
      [WARN] :   W/System.err 	at android.os.Looper.loop(Looper.java:136)
      [WARN] :   W/System.err 	at 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
      

      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

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

      ./Resources/ExampleService.js

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

      Replace android element with the following in tiapp.xml

          <android xmlns:android="http://schemas.android.com/apk/res/android">
              <services>
                  <service url="ExampleService.js" type="interval"/>
              </services>
          </android>
      

        Attachments

          Activity

            People

            • Assignee:
              msamah Ashraf Abu (Inactive)
              Reporter:
              bhatfield Benjamin Hatfield (Inactive)
              Reviewer:
              Hieu Pham
            • Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Backbone Issue Sync

                • Titanium SDK/CLI <> Titanium Mobile
                  Synced with:
                  TIMOB-19405
                  Sync status:
                  ERROR
                  Last received:
                  Last sent:

                  Git Integration