具有不同消息的iOS间隔本地通知

时间:2016-06-20 13:57:09

标签: swift push-notification

我想每小时发送一次本地通知(仅一天),使用不同的消息。当新人来时,也想要解除先前的通知。

我使用此代码但是第二个通知带有与第一个相同的文本! 并且不会解雇前一个......

   UIApplication.sharedApplication().cancelAllLocalNotifications()
    var Text = "!!!!!!!!!"
    let Texts = [
        "11111111111",
        "22222222222",
        "33333333333",
        "44444444444",
        "55555555555"
    ]

    Text = Texts[Int(arc4random_uniform(UInt32(Texts.count)))]



    let localNotification:UILocalNotification = UILocalNotification()
    localNotification.alertAction = "Testing notifications on iOS8"
    localNotification.alertBody = Text
    localNotification.fireDate = NSDate(timeIntervalSinceNow: 1)
    localNotification.soundName = UILocalNotificationDefaultSoundName
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)


    let localNotification2:UILocalNotification = UILocalNotification()
    localNotification2.alertAction = "Testing notifications on iOS8"
    localNotification2.alertBody = Text
    localNotification2.fireDate = NSDate(timeIntervalSinceNow: 5)
    localNotification2.soundName = UILocalNotificationDefaultSoundName
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification2)

1 个答案:

答案 0 :(得分:0)

我创建了一个从方法中获取文本的方法也许可以解决问题

internal func RandomText(Text: String) -> (String){

var Text = "!!!!!!!!!"
let Texts = [
    "11111111111",
    "22222222222",
    "33333333333",
    "44444444444",
    "55555555555"
]

Text = Texts[Int(arc4random_uniform(UInt32(Texts.count)))]
return(Text)

}

并将警报文本更改为:

localNotification2.alertBody = String(RandomText)

但是这总是回归:(功能)