0

I'm trying to pass a variable to my backgroundService.js and them set the push time through this variable... but it is not working... only work if I manually set the time in milliseconds ...

// app.js

var timer = 2000

Ti.App.Properties.setString( 'pushTime', timer )

var service = Ti.App.iOS.registerBackgroundService({
    url:'bg.js'
})


// bg.js

var timer = Ti.App.Properties.getString( 'pushTime' )
Ti.API.info( timer ) 

var notification = Ti.App.iOS.scheduleLocalNotification({
    alertBody:"Passou a bebedeira ja?",
    alertAction:"Ok",
    userInfo:{"hello":"world"},
    sound:"pop.caf",
    badge: 1,
    date:new Date(new Date().getTime() + timer )
})

someone knows why when I use timer instead 2000 it not work?

Thanks 3.1.1 iOS

DHennrich
  • 45
  • 8
  • I don't know if I understand you but my code should work... since I change my `timer` to `2000` it make the push... on Titanium's Doc for this params of notification I get this: date : Date Date and time for the notification to occur. – DHennrich Aug 02 '13 at 02:24
  • `var timer = Ti.App.Properties.getString('pushTime')`. Will Javascript automatically cast this to a number?? – borrrden Aug 02 '13 at 02:33
  • yea on `Ti.API.info( timer )` it returns `2000` so it is a number – DHennrich Aug 02 '13 at 03:02
  • That doesn't convince me. Why would a method called "getString" return a number? If that's really the case it is no wonder people hate Javascript. Why don't you try "getInt" and "setInt" instead, just for fun... – borrrden Aug 02 '13 at 03:32
  • @borrrden: getString() won't return a number value. You can see it here. http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.App.Properties-method-getString. The console displays 2000 but it's type is string – Anand Aug 02 '13 at 03:44

1 Answers1

1

DHennrich, Ti.API.info( timer ) returns 2000 doesn't mean that 2000 is a type of number. Actually it is a string since you're using setString method to store the value and retrieve it using getString(). When you use setString it stores the value as a string and getString always returns a string avlue. That's why your code worked when you used 2000 instead of timer variable.

From API Documentation

getString( String property, [String default] ) : String

Returns the value of a property as a string data type. Parameters

property : String

Name of property.
default : String (optional)

Default value to return if property does not exist.

Returns

String

You can use setInt or setDouble instead of setString to set a property and to retrieve use getInt or getDouble respectively.

Try to store the value using Ti.App.Properties.setInt( 'pushTime', timer );

and you can retrieve the value using

var timer = Ti.App.Properties.getInt( 'pushTime' );

I hope it resolved your issue.

Anand
  • 5,323
  • 5
  • 44
  • 58