Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-26237] iOS: Local Notification Event not triggered when app is launched from the background

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionFixed
Resolution Date2018-08-21T07:12:26.000+0000
Affected Version/sRelease 6.3.0, Release 7.1.1, Release 7.2.0
Fix Version/sRelease 7.3.1
ComponentsiOS
Labelslocalnotification, pushnotification
ReporterMatthew Delmarter
AssigneeHans Knöchel
Created2018-07-24T01:47:02.000+0000
Updated2018-09-19T21:03:18.000+0000

Description

If a local notification is shown and the user taps the notification, the app opens, but the notification event is not triggered. It is triggered just fine if the app has already been opened but is not currently active in the foreground. Sample code - the alerts are never fired if tapping the notification is staring up the app:
Ti.App.iOS.registerUserNotificationSettings({
	types: [
		Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT
	]
});

Ti.App.iOS.addEventListener('notification', function(e) {
	alert('notification');
});

Ti.App.iOS.addEventListener('localnotificationaction', function(e) {
	alert('localnotificationaction');
});

Ti.App.addEventListener('pause',function()
{
	var timestamp = new Date(new Date().getTime() + 10000);

	// The following code snippet schedules an alert to be sent within three seconds
	var notification = Ti.App.iOS.scheduleLocalNotification({
		category: "TEST",
		alertAction: 'Read now',
		alertBody: 'Custom message',
		date: timestamp,
		repeat: "daily",
		//The following URL is passed to the application
		userInfo: {
			"task": "test"
		}
	});
});

Comments

  1. Hans Knöchel 2018-07-24

    Taken (and modified) from TIMOB-23527 (7.3.0), the following works fine:
       // An action that opens the app (foreground action)
       var acceptAction = Ti.App.iOS.createUserNotificationAction({
           identifier: 'ACCEPT_IDENTIFIER',
           title: 'Accept',
           activationMode: Ti.App.iOS.USER_NOTIFICATION_ACTIVATION_MODE_FOREGROUND,
           destructive: false,
           authenticationRequired: true
         });
         
         // An action that does not open the app (background action)
         var rejectAction = Ti.App.iOS.createUserNotificationAction({
           identifier: 'REJECT_IDENTIFIER',
           title: 'Reject',
           activationMode: Ti.App.iOS.USER_NOTIFICATION_ACTIVATION_MODE_BACKGROUND,
           destructive: true,
           authenticationRequired: false
         });
         
         // An action that does not open the app (background action), but lets the developer
         // type in a text (iOS 9+)
         var respondAction = Ti.App.iOS.createUserNotificationAction({
           identifier: 'RESPOND_IDENTIFIER',
           title: 'Respond',
           activationMode: Ti.App.iOS.USER_NOTIFICATION_ACTIVATION_MODE_BACKGROUND,
           behavior: Ti.App.iOS.USER_NOTIFICATION_BEHAVIOR_TEXTINPUT, // or: Ti.App.iOS.USER_NOTIFICATION_BEHAVIOR_DEFAULT,
           authenticationRequired: false
         });
         
         // Create an example notification category
         var downloadContent = Ti.App.iOS.createUserNotificationCategory({
           identifier: 'DOWNLOAD_CONTENT',
           actionsForDefaultContext: [acceptAction, rejectAction, respondAction]
         });
         
         // Register for user notifications and categories
         Ti.App.iOS.registerUserNotificationSettings({
           types: [
             Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT
           ],
           categories: [downloadContent]
         });
         
         // Monitor notifications received while app is in the background
         Ti.App.iOS.addEventListener('localnotificationaction', function(e) {
           alert('event: localnotificationaction');
         });
         
         Ti.App.iOS.addEventListener('usernotificationsettings', function(e) {
           alert('event: usernotificationsettings');
         })
         
         // Monitor notifications received while app is in the foreground
         Ti.App.iOS.addEventListener('notification', function(e) {
           alert('event: notification');
         });
         
         // App UI
         var win = Ti.UI.createWindow({
           backgroundColor: 'white'
         });
         var button = Ti.UI.createButton({
           title: 'Trigger Notification'
         });
       
         button.addEventListener('click', function(e) {
           // Send a notification in 3 seconds
           var note = Ti.App.iOS.scheduleLocalNotification({
             date: new Date(new Date().getTime() + 3000),
             alertTitle: 'My download',
             alertSubtitle: 'Check it out!',
             alertBody: 'New content available! Download now?',
             badge: 1,
             userInfo: {
               'url': 'http://www.download.com/resource/asset.json',
               id: '1'
             },
             category: 'DOWNLOAD_CONTENT'
           });
         });
           
         win.add(button);
         win.open();
       
  2. Matthew Delmarter 2018-07-24

    Thanks for the quick reply Hans! To clarify (without having played with your code yet), does the link to TIMOB-23527 (7.3.0) mean that I need to wait for 7.3.0 for this to work? Or it should already work ok in 7.2.0 & 7.1.0 etc?
  3. Hans Knöchel 2018-07-24

    I tried with 7.3.0, but it's also supposed to work in <= 7.2.0. *EDIT*: Just tested with 7.2.0, works fine as well. Please compare the code to identify the issue for you. Thanks!
  4. Matthew Delmarter 2018-07-24

    I am not having any luck here Hans. To be clear, the issue is about the notification event not being fired +from a fresh launch+. I have disabled the eventListener for *usernotificationsettings* as this always fires when the app is opened even if a notification has not been triggered. And from what I can see it is not relevant to a +specific+ notification being triggered by the user. Doing this step by step using your sample code... This process *works ok*: 1. Open the app 2. Click the "Trigger Notification" button 3. Home button once 4. Notification appears - click to Open. Note: Don't trigger the Accept/Reject/Respond, just tap the notification once to Open the app. 5. App opens and the alert "event: notification" appears This process *does not work*: 1. Open the app 2. Click the "Trigger Notification" button 3. Home button twice, to force close the app 4. Notification appears - click to Open. Note: Don't trigger the Accept/Reject/Respond, just tap the notification once to Open the app. 5. App opens - no alert is triggered I was testing with a clean app using 7.2.0 SDK. Can you let me know what is different for you? Does the second process, after a force close, work ok for you? Or am I missing something?
  5. Hans Knöchel 2018-07-26

    Hey there, you have been right! This is a quite edgy case that doesn't seem to be handled so far. I tested out a fix locally and it works fine. I will try to get it into 7.3.1 (as 7.3.0 is due next week already), but will also provide you a patch that you can apply today to test it out. More soon!
  6. Hans Knöchel 2018-07-26

    PR (master): https://github.com/appcelerator/titanium_mobile/pull/10213 PR (7_3_X): https://github.com/appcelerator/titanium_mobile/pull/10215 Test-Case: In the comments above
  7. Hans Knöchel 2018-07-27

    Master merged, 7.3.1 will be merged once 7_3_X is bumped.
  8. Eric Wieber 2018-08-22

    Verified fixed and changes are included in SDK builds 7.3.1.v20180821233955 & 7.4.0.v20180822015105
  9. Matthew Delmarter 2018-09-17

    Unfortunately there is a bug with this fix. The fix is working perfectly, until one scenario... If the notification has the optional sound parameter set, then when the notification event is triggered, the following error is thrown... {noformat} [ERROR] Application raised an exception: -[UNNotificationSound boundBridge:withKrollObject:]: unrecognized selector sent to instance 0x600003336d00` {noformat} And the "localnotificationaction" event listener is not triggered. If the notification has the sound parameter removed, then everything works as expected. Unfortunately my app does have the sound parameter defined, and my users are well trained to hear the alert sound. If there is any chance this could be fixed as soon as possible I would be profoundly grateful :) To clarify here is the same notification as shown in the sample code above, but with the sound parameter defined:
       button.addEventListener('click', function(e) {
         // Send a notification in 3 seconds
         var note = Ti.App.iOS.scheduleLocalNotification({
           date: new Date(new Date().getTime() + 3000),
           alertTitle: 'My download',
           alertSubtitle: 'Check it out!',
           alertBody: 'New content available! Download now?',
           sound: "/alert.wav", // <<<<< This causes an issue
           badge: 1,
           userInfo: {
             'url': 'http://www.download.com/resource/asset.json',
             id: '1'
           },
           category: 'DOWNLOAD_CONTENT'
         });
       });
       
  10. Matthew Delmarter 2018-09-17

    Further to my above comment, in same cases this error will crash the app.
  11. Matthew Delmarter 2018-09-19

    I have created a new ticket for the issue: AC-5901

JSON Source