ios - Titanium - iOS 后台服务不工作

标签 ios titanium background-service

我试图将一个变量传递给我的backgroundService.js,他们通过这个变量设置推送时间...但它不起作用...只有当我手动设置时间(以毫秒为单位)时才有效...

// 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 )
})

有人知道为什么当我使用timer而不是2000时它不起作用?

谢谢 3.1.1 iOS

最佳答案

DHennrich,Ti.API.info(timer) 返回 2000 并不意味着 2000 是一种数字。实际上它是一个字符串,因为您使用的是 setString方法来存储值并使用 getString() 检索它。当您使用 setString 时,它将值存储为字符串,而 getString 始终返回字符串 avlue。这就是为什么当您使用 2000 而不是 timer 变量时您的代码可以工作。

来自 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

您可以使用 setIntsetDouble而不是 setString 来设置属性并检索使用 getIntgetDouble分别。

尝试使用 Ti.App.Properties.setInt( 'pushTime', timer ); 存储值

您可以使用

检索该值

vartimer = Ti.App.Properties.getInt('pushTime');

希望它能解决您的问题。

关于ios - Titanium - iOS 后台服务不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18007201/

相关文章:

ios - 为什么在创建项目范围的常量时需要实现文件 (.m)?

ios - 在 Swift 3 iOS 中进行 HTTP POST 时出现未知错误

javascript - Appcelerator Titanium 中的文本字段验证

java - 将 Visual Studio Code 与 Titanium 结合使用时出现 "JDK (Java Development Kit) not Installed"

android - 如何运行后台服务以在android中发出警报?

ios - 将 isTouchInside 设置为 false

ios - 关于 UIScrollView 作为启用 UIPanGesture 的 View 的 subview 的问题

javascript - 如何识别照片库中的 GIF 图片 (iOS)

java - 如何在 android 中使用后台服务触发 wifi 状态更改?

android - 在后台持续更新位置