[TIMOB-18182] iOS: Distinguish foreground vs. background in Local Notification
GitHub Issue | n/a |
---|---|
Type | Improvement |
Priority | Medium |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2017-07-20T01:03:41.000+0000 |
Affected Version/s | Release 3.4.0, Release 3.4.1 |
Fix Version/s | Release 6.2.0 |
Components | iOS |
Labels | TCSupport |
Reporter | Shawn Lan |
Assignee | Hans Knöchel |
Created | 2014-08-15T21:54:00.000+0000 |
Updated | 2017-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.
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 SimulatorCODE SAMPLE
app.js
bg.js
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.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.
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
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.
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.Changes verified to be in SDK's: 6.2.0.v20170810134640 7.0.0.v20170808160733