Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-18182] iOS: Distinguish foreground vs. background in Local Notification

GitHub Issuen/a
TypeImprovement
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2017-07-20T01:03:41.000+0000
Affected Version/sRelease 3.4.0, Release 3.4.1
Fix Version/sRelease 6.2.0
ComponentsiOS
LabelsTCSupport
ReporterShawn Lan
AssigneeHans Knöchel
Created2014-08-15T21:54:00.000+0000
Updated2017-08-10T22:36:59.000+0000

Description

Right now there is no way to determine whether a local notification is received when the app is in the foreground or in the background. Here is a sample:
var notification = Ti.App.iOS.scheduleLocalNotification({
    alertAction: 'Check',
    alertBody: 'This is a test',
    date: new Date(new Date().getTime() + 3000)
});

Ti.App.iOS.addEventListener('notification', function(e) {
    Ti.API.info(JSON.stringify(e));
    notification.cancel();
});
The passed-through event object is the same for both situations. However, with remote notification you have such ability: http://docs.appcelerator.com/titanium/latest/#!/api/PushNotificationData-property-inBackground From Apple's docs, both remote and local notifications are handled similarly. You'll be able to tell whether a local notification is received in the foreground or the background if coding in Objective-C, but not in Titanium. This is important since actions taken may need to be different accordingly. E.g. When in the foreground you might just want to display a notification, but not to interrupt what users are doing. When in the background, and users click the notification to launch/resume the app, you want to take the users directly to the action. Thanks.

Comments

  1. Amimul Hossain 2014-08-19

    Hello, We have tested the issue in Ti SDK 3.3.0.GA and iOS local Notification is working when the application goes into background.

    TESTING ENVIRONMENT

    CLI version 3.3.0, Titanium SDK version 3.3.0.GA iOS Device, iPhone 5S, iOS Simulator

    CODE SAMPLE

    app.js

       var win = Ti.UI.createWindow({
       	backgroundColor:'white'
       });
       win.open();
       var label = Ti.UI.createLabel({
       	top: 20,
       	height: 200,
       	width: 200,
       	text: "Background the app"
       });
       win.add(label);
        
       function isiOS4Plus()
       {
       	// add iphone specific tests
       	if (Titanium.Platform.name == 'iPhone OS')
       	{
       		var version = Titanium.Platform.version.split(".");
       		var major = parseInt(version[0],10);
       		
       		// can only test this support on a 3.2+ device
       		if (major >= 4)
       		{
       			return true;
       		}
       	}
       	return false;
       }
        
       if (isiOS4Plus())
       {
       	// register a background service. this JS will run when the app is backgrounded but screen is OFF!!!
       	var service = Ti.App.iOS.registerBackgroundService({url:'bg.js'});
        
       	Ti.API.info("registered background service = "+service);
        
       	// listen for a local notification event
       	Ti.App.iOS.addEventListener('notification',function(e)
       	{
       		Ti.API.info("local notification received: "+JSON.stringify(e));
       	});
        
       	// fired when an app resumes for suspension
       	Ti.App.addEventListener('resume',function(e){
       		Ti.API.info("app is resuming from the background");
       	});
       	Ti.App.addEventListener('resumed',function(e){
       		Ti.API.info("app has resumed from the background");
       	});
        
       	//This event determines that the app it was just paused
       	Ti.App.addEventListener('pause',function(e){
       		Ti.API.info("app was paused from the foreground");
       	});
       }
       

    bg.js

       
       
       var value = "Hello from Running BG service - bg.js";
       Ti.API.info(value);
        
       var notification = Ti.App.iOS.scheduleLocalNotification({
       	alertBody:"App was put in background",
       	alertAction:"Re-Launch!",
       	userInfo:{"hello":"world"},
       	date:new Date(new Date().getTime() + 3000) // 3 seconds after backgrounding
       });
        
       Ti.App.iOS.addEventListener('notification',function(){
       	Ti.API.info('background event received = '+notification);
       	//Ti.App.currentService.unregister();
       });
        
       

    STEP TO TEST

    - Create a new Classic Project - Create a new js file named "bg.js" in your projects resource folder. - Copy the "app.js" code segment to your project "app.js" file. - Copy the "bg.js" code segment to your project "bg.js" file. - Run the application in iPhone Device or Simulator.

    EXPECTED RESULT

    When the application run, Put the application on background. A notification pops up after some seconds saying "App was put in background". Tapping on the notification will take the app into foreground. Thanks.
  2. Shawn Lan 2014-09-04

    Sorry, but you misunderstand my question. First, no background service is needed. Just create any app using my example code in the index.js. Two scenarios here: 1. After launching the app, press "Home" to put the app back to the background immediately. Now after three seconds, the notification shows, appearing in the notification center. Clicking the notification to bring back the app to the foreground, and then the "notification" event is fired. 2. After launching the app, don't press "Home." Now the app is staying in the foreground. After three seconds, the "notification" event is fired. No notification appear in the notification center in this case. Now, in the "notification" event handler, how can I distinguish the above two scenarios? The action taken can be different. For #1, it can take the user directly to the action. For #2, it may just pop up some messages. I suggest you do the same as for the push notification. It has an "inBackground" event property for this purpose. At this moment, there is no way to distinguish these two scenarios for a local notification.
  3. Mauro Parra-Miranda 2014-12-10

    Hello [~shawnlan]: Can you please provide a testcase that shows the issue? If you can provide one in Ti Classic is a lot better, so we can take Alloy out of the picture, simplyfing the problem. Best Regards
  4. Shawn Lan 2014-12-11

    This is not a bug. It doesn't matter Alloy or not. It's just some missing feature, at least I can't find it in your docs. Push Notification - you can determine if the notification received in the background or foreground. Local Notification - "no way" to determine if the notification received in the background or foreground. However, these two types of notifications should be treated the same way, according to Apple's docs. That's why I suggest add an "inBackground" property to the local notification event, like you did for push notification.
  5. Hans Knöchel 2017-06-29

    PR: https://github.com/appcelerator/titanium_mobile/pull/9180 Demo: See the provided test-cases. There will be a new property inBackground indicating whether or not the notification was received in the background or foreground.
  6. Abir Mukherjee 2017-08-10

    Changes verified to be in SDK's: 6.2.0.v20170810134640 7.0.0.v20170808160733

JSON Source