Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-16492] Android: Not able to capture intent data from a Notification created from Background service

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionFixed
Resolution Date2014-03-19T19:51:38.000+0000
Affected Version/sRelease 3.2.1
Fix Version/s2014 Sprint 06, 2014 Sprint 06 SDK, Release 3.3.0
ComponentsAndroid
Labelsmodule_android, qe-closed-3.3.0, qe-manualtest, supportTeam
ReporterJörgen Buder
AssigneeSunila
Created2014-02-18T15:03:34.000+0000
Updated2014-08-05T23:24:30.000+0000

Description

Issue description

I am using a simple background service to create a notification. The notification intent has extra data to it, and is then fired. The notification is displayed in the status bar. I pull down the bar and my app is opened/resumed. I try to read the intent data, but it appears as if the app is not resuming and even if I try to get both resume(d) and or the open on the activity I am not able to read any intent data added from the background service. Please note that even if the service is registered again at next start the notification should still be possible to catch. I have read all things I can get, but I have not found any instruction on how to capture the notification intent data. All guides stop where it has appeared in the status bar, even den android dev ones.

Test outcomes

- Data intent is only retrieved when you get it through android service resource: "app/lib/background_locationservice_android.js"
//DISPLAYS ALERT BOX ("TEST2")
var service = Ti.Android.currentService;
if (service) {
	var intent = service.getIntent();
	var teststring = intent.getStringExtra("message");
	alert(teststring);

	service.addEventListener("taskremoved", function() {
		Ti.Android.NotificationManager.cancelAll();
		Ti.Android.stopService(intent);
	});
}
However you can't get intent data either in app/alloy.js or controllers/index.js

Steps to reproduce

Get and run stripped down version on Android device.

Wait until a notification get trigger.

Quit app then click at notification at the statusBar.

Monitor Logs. Depending where was getStringExtra set will be the outcome.

Sample: https://www.dropbox.com/s/wow58vtprsiktu3/Arkiv_TestCase.zip

Attachments

FileDateSize
Arkiv.zip2014-02-18T15:03:34.000+00005903193

Comments

  1. Sunila 2014-02-24

    Exposed a new method to expose root activity. Only the intent as part of the launched activity will have the extra data. Right now after a window is opened, there is no method to access the root activity. Existing code should work if 'Ti.Android.currentActivity' is called before window is opened. Something like
       var activity = Ti.Android.currentActivity;
       var intent = activity.getIntent();
       Ti.API.info('interval = '+intent.getIntExtra('interval',0)+' message='+intent.getStringExtra('message'));
       
    or use the new API 'getRootActivity' inside 'open' to get the data Something like
       var rootActivity = Ti.Android.getRootActivity();
       var rootIntent = rootActivity.getIntent();
       Ti.API.info('RootIntent message='+rootIntent.getStringExtra('message'));
       
    PR https://github.com/appcelerator/titanium_mobile/pull/5368
  2. Sunila 2014-03-19

    New pull request based on latest master. https://github.com/appcelerator/titanium_mobile/pull/5493 Usage var rootIntent = Ti.App.Android.launchIntent; Ti.API.info('RootIntent message='+rootIntent.getStringExtra('message'));
  3. Vishal Duggal 2014-03-19

    Test Code
       var launchIntent = Ti.Android.currentActivity.intent; 
       
       var checkInWindow = false;
       var testMessage = launchIntent.getStringExtra("message");
       if(testMessage != null) {
       	checkInWindow = true;
       }
       
       var win = Ti.UI.createWindow({
       	backgroundClor:'white',
       	exitOnClose:true
       });
       
       if(checkInWindow == true) {
       	
       	var l1 = Ti.UI.createLabel({top:50,text:testMessage});
       	var l2 = Ti.UI.createLabel({bottom:50,text:''});
       	var l3 = Ti.UI.createLabel({text: 'VALUES MUST BE SAME'});
       	
       	win.add(l1);
       	win.add(l2);
       	win.add(l3);
       	
       	win.addEventListener('open',function(e){
       		l2.text = Ti.App.Android.launchIntent.getStringExtra("message");
       	});
       	
       } else {
       	var btn = Ti.UI.createButton({title:'CREATE PENDING INTENT'});
       	win.add(btn);
       	btn.addEventListener('click',function(e){
       		Ti.API.info('BUTTON CLICK');
       
       		var intent = Ti.Android.createIntent({
       		    flags : Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_NEW_TASK,
       		    // our application app id activity class. Replace as required
       		    className : 'com.appcelerator.titaniumtest.TitaniumtestActivity'
       		});
       		intent.putExtra('message', "This is test string");
       		intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);
       		Ti.API.info('Created intent');
       		// Create a PendingIntent to tie together the Activity and Intent
       		var pending = Titanium.Android.createPendingIntent({
       		    intent: intent,
       		    flags: Titanium.Android.FLAG_UPDATE_CURRENT
       		});
       		Ti.API.info('Created pending intent');
       		// Create the notification
       		var notification = Titanium.Android.createNotification({
       		    // icon is passed as an Android resource ID -- see Ti.App.Android.R.
       		    //icon: 'images/ic_search.png',
       		    contentTitle: 'Test Intent',
       		    contentText : 'Click to return to the application.',
       		    contentIntent: pending
       		});
       		Ti.API.info('Created notification');
       		// Send the notification.
       		Titanium.Android.NotificationManager.notify(1, notification);
       		Ti.API.info('notification sent');
       		
       		setTimeout(function(){
       			win.activity.finish();
       		},200);
       		
       	});
       }
       
       win.open();
       
  4. Paras Mishra 2014-04-28

    Intent data is successfully captured from a Notification created from Background service, tested using the test code by Vishal Duggal. Intent message is displayed on app screen when clicking the notification in the notification bar. Verified the fix on: Device : Google Nexus 4, Android Version: 4.1.1 SDK: 3.3.0.v20140425191906 CLI version : 3.3.0-dev OS : MAC OSX 10.9.2 Alloy: 1.4.0-dev ACS: 1.0.14 npm:1.3.2 Appcelerator Studio, build: 3.3.0.201404251359 titanium-code-processor: 1.1.1-alpha XCode : 5.1.1

JSON Source