我是iOS编程的新手,在我的应用程序中,我有一个必须在某个时间出现的UILocaleNotification。
首先我设置参数:
let notificationSettings: UIUserNotificationSettings! = UIApplication.sharedApplication().currentUserNotificationSettings()
if (notificationSettings.types == UIUserNotificationType.None) {
var notificationTypes: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Sound
// Bouton d'envoi
var sendAction = UIMutableUserNotificationAction()
sendAction.identifier = "send"
sendAction.title = "Send"
sendAction.activationMode = UIUserNotificationActivationMode.Foreground
sendAction.destructive = false
sendAction.authenticationRequired = true
// Bouton de non-envoi
var dontSendAction = UIMutableUserNotificationAction()
dontSendAction.identifier = "dontsend"
dontSendAction.title = "Don't send"
dontSendAction.activationMode = UIUserNotificationActivationMode.Background
dontSendAction.destructive = true
dontSendAction.authenticationRequired = false
let actionsArray = NSArray(objects: sendAction, dontSendAction)
var reminderNotification = UIMutableUserNotificationCategory()
reminderNotification.identifier = "reminderNotification"
reminderNotification.setActions(actionsArray as [AnyObject], forContext: UIUserNotificationActionContext.Minimal)
let categoriesForSettings = NSSet(objects: reminderNotification)
let newNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: categoriesForSettings as Set<NSObject>)
UIApplication.sharedApplication().registerUserNotificationSettings(newNotificationSettings)
}
...我按照这样安排通知:
var localNotification = UILocalNotification()
localNotification.fireDate = NSDate(timeIntervalSinceNow: 3)
localNotification.timeZone = NSTimeZone.defaultTimeZone()
localNotification.alertBody = "Hey, inform your contacts !"
localNotification.alertAction = "Send Message"
localNotification.category = "reminderNotification"
localNotification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
当我尝试此代码时,通知显示在通知中心,但没有弹出显示... 我在网上搜索来源,我不明白为什么这些例子和tutos显示弹出窗口,因为我写了相同的代码......
感谢您的帮助,祝您度过愉快的一天!
编辑:我忘记了一些事情:通知中心必须检测事件,而不是区域设置通知。所以我希望通知中心的addObserver检查事件,是否可能处于后台状态?答案 0 :(得分:0)
根据此行: <parameter name="transport.vfs.FileURI">file:///home/user/test/in</parameter> <!--CHANGE-->
<parameter name="transport.vfs.ContentType">text/xml</parameter>
<parameter name="transport.vfs.FileNamePattern">.*\.xml</parameter>
<parameter name="transport.PollInterval">15</parameter>
<parameter name="transport.vfs.MoveAfterProcess">file:///home/user/test/original</parameter> <!--CHANGE-->
<parameter name="transport.vfs.MoveAfterFailure">file:///home/user/test/original</parameter> <!--CHANGE-->
<parameter name="transport.vfs.ActionAfterProcess">MOVE</parameter>
<parameter name="transport.vfs.ActionAfterFailure">MOVE</parameter>
<target>
<endpoint>
<address format="soap12" uri="http://localhost:9000/services/SimpleStockQuoteService"/>
</endpoint>
<outSequence>
<property name="transport.vfs.ReplyFileName"
expression="fn:concat(fn:substring-after(get-property('MessageID'), 'urn:uuid:'), '.xml')"
scope="transport"/>
<property action="set" name="OUT_ONLY" value="true"/>
<send>
<endpoint>
<address uri="vfs:file:///home/user/test/out"/> <!--CHANGE-->
</endpoint>
</send>
</outSequence>
</target>
<publishWSDL uri="file:repository/samples/resources/proxy/sample_proxy_1.wsdl"/>
</proxy>
您希望在3秒后触发通知。我认为应用程序在发生时仍处于前台。如果是这样,您将不会有通知弹出窗口。
如果在收到通知时打开了与通知关联的应用,则必须在localNotification.fireDate = NSDate(timeIntervalSinceNow: 3)
中处理该应用以显示自定义弹出窗口。
AppDelegate