热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在视图中安排本地通知确实加载方法-Schedulelocalnotificationsintheviewdidloadmethod

imanoobtoprogrammingonswift,andimfacingaproblem.Imtryingtoschedulealocalnotific

i'm a noob to programming on swift, and i'm facing a problem. I'm trying to schedule a local notification every day at the same hour, this without the user scheduling it. Some how the repeat interval method gives me more that one notification at the time. So i don't now what's happening. Basically what i want is to schedule the notification properly and call the schedule method in the right way.

我是一个快速编程的菜鸟,我正面临着一个问题。我正在尝试每天在同一时间安排本地通知,这没有用户安排它。一些重复间隔方法如何为我提供更多的通知。所以我现在不知道发生了什么。基本上我想要的是正确安排通知并以正确的方式调用调度方法。

This is my view controller method where i call the notification

这是我的视图控制器方法,我调用通知

      override func viewDidLoad() {
      super.viewDidLoad()
      notificar.scheduleNotification("message")     
}

This is the notification helper class

这是通知助手类

class NotificationHelper {

static func askPermission() {

    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}



 func scheduleNotification(InputUser:String) {

    let now: NSDateCompOnents= NSCalendar.currentCalendar().components([.Hour, .Minute], fromDate: NSDate())

    let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let date = cal.dateBySettingHour(now.hour, minute: now.minute + 1, second: 0, ofDate: NSDate(), options: NSCalendarOptions())
    let reminder = UILocalNotification()
    reminder.fireDate = date
    reminder.alertBody = InputUser
    reminder.alertAction = "Cool"
    reminder.soundName = "sound.aif"
    reminder.repeatInterval = NSCalendarUnit.Minute




    UIApplication.sharedApplication().scheduleLocalNotification(reminder)

    print("Firing at \(now.hour):\(now.minute+1)")
}

And this is my app delegate

这是我的应用代表

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.


    NotificationHelper.askPermission()

    return true
}

As you can see, in the code im calling every minute the notification repeat interval, but as soon as the notification gets called i recive an stack of 5 notifications in a row, not single one.

正如你所看到的,在代码中我每分钟都会调用通知重复间隔,但是一旦调用了通知,我就会连续收到一堆5个通知,而不是单个通知。

please any ideas, i will be very grateful. And sorry for my english. thanks :)

请任何想法,我将非常感激。抱歉我的英语。谢谢 :)

1 个解决方案

#1


0  

You have to remove local notification first and then add a new one here is the code.

你必须首先删除本地通知,然后在这里添加一个新的代码。

var app: UIApplication = UIApplication.sharedApplication()
var eventArray: [AnyObject] = app.scheduledLocalNotifications()
for oneEvent: UILocalNotification in eventArray 
{
    var userInfoCurrent: [NSObject : AnyObject] = oneEvent.userInfo
    var uid: String = "\(userInfoCurrent["name"])"
// here give the name of your local notification.
    if (uid == obj["name"]) 
    {
        //Cancelling local notification
        app.cancelLocalNotification(oneEvent)
    }
}

推荐阅读
author-avatar
有些疯癫的小红帽
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有