Titanium JIRA Archive
Appcelerator Community (AC)

[AC-6036] iOS: image placed in the applicationDataDirectory is removed if used as an attachment in a scheduleLocalNotification

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionInvalid
Resolution Date2018-11-28T18:35:40.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
Labelsios, localnotification, notification, notifications, schedulelocalnotification
ReporterVittorio Sorbera
AssigneeShak Hossain
Created2018-11-27T14:32:56.000+0000
Updated2018-11-28T18:35:40.000+0000

Description

I'm trying to attach an image, placed in the _Ti.Filesystem.applicationDataDirectory_, to a local notification. But as soon as I do the _Ti.App.iOS.scheduleLocalNotification()_, the image is removed from the _Ti.Filesystem.applicationDataDirectory_ Example: first click on "Capture screen", and then on "Shedule notification"
var win = Ti.UI.createWindow({
	backgroundColor : "yellow",
	layout : "vertical"
});

var imageView =  Ti.UI.createImageView({
	top : 50,
	width : 200,
	height : 200,
	backgroundColor : "white",
	borderWidth : 5,
	borderColor : "white"
});

var captureScreenBtn = Ti.UI.createButton({
	top : 20,
	title : "Capture screen",
	width : Ti.UI.SIZE,
	height : Ti.UI.SIZE
});

var notificationBtn = Ti.UI.createButton({
	top : 20,
	title : "Schedule notification",
	width : Ti.UI.SIZE,
	height : Ti.UI.SIZE
});

captureScreenBtn.addEventListener("click",function(){
	var imageBlob = win.toImage(null,true);
	var imageUrl = Ti.Filesystem.applicationDataDirectory + "image.jpg";
	var f = Ti.Filesystem.getFile(imageUrl);
	f.write(imageBlob);
	imageView.image = imageUrl;
	imageExists();
});

notificationBtn.addEventListener("click",function(){
	Ti.App.iOS.scheduleLocalNotification({
		date: new Date(new Date().getTime() + 5000),
		alertBody: "Test notification",
		badge: 1,
		userInfo: {id:1},
		alertLaunchImage : "appicon.png",
		sound : "default",
		attachments: [{
			identifier: "1",
			url: Ti.Filesystem.applicationDataDirectory + "image.jpg"
		}],
		identifier: "1"
	});
	setTimeout(function(){
	    imageView.image = Ti.Filesystem.applicationDataDirectory + "image.jpg"
	    imageExists();
	},100)
	
});

function imageExists(){
	var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory + "image.jpg");
	alert('"image.jpg" exists? ' + f.exists());
}

win.addEventListener('open', function() {
  Ti.App.iOS.registerUserNotificationSettings({
    types: [
      Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT,
      Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND,
      Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE
    ]
  });
});

win.add(imageView);
win.add(captureScreenBtn);
win.add(notificationBtn);
win.open();

Comments

  1. Vittorio Sorbera 2018-11-27

    I've also tried with files placed in applicationCacheDirectory and tempDirectory. Also in this case the image is removed. I have also tried with PNG files, but the problem persists. The issue does not occur if the image is placed in assets folder instead
  2. Jan Vennemann 2018-11-28

    That's native behavior if you try to attach files. iOS will validate the files you try to attach and move them afterwards. Please see the following note for [UNNotificationAttachment](https://developer.apple.com/documentation/usernotifications/unnotificationattachment?language=objc#overview) {quote}The system validates attachments before displaying the associated notification. If you attach a file to a local notification request that is corrupted, invalid, or of an unsupported file type, the system doesn't schedule your request. For remote notifications, the system validates attachments after your notification service app extension finishes. Once validated, attached files are moved into the attachment data store so that they can be accessed by all of the appropriate processes. Attachments located inside an app’s bundle are copied instead of moved.{quote} This explains why the files in the asset folder are still there, but getting moved from all other directories. Create a copy of the file if you wan't to attach to avoid that your original file gets moved.
  3. Vittorio Sorbera 2018-11-28

    Thanks, now it's clear. At this point it is convenient save the image in the applicationDataDirectory and, just before the scheduleLocalNotification(), copy it in the tempDirectory and use this copy as image to attach. Thus the image should be first correctly scheduled and then removed

JSON Source