如何在Swift 3中的特定日期发送通知

时间:2017-02-09 10:22:00

标签: swift3 xcode8 ios10

我想在每月的第一天向用户发送通知。我怎样才能做到这一点?这是一个只在本地而非远程的通知。

2 个答案:

答案 0 :(得分:0)

您应该在特定日期使用此代码进行通知。

let alarmTime = Date().addingTimeInterval(60 * 60 * 24 * 7 - 300) /// you should make changes here according to your requirement.
let components = Calendar.current.dateComponents([.weekday, .hour, .minute], from: alarmTime)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)


let content = UNMutableNotificationContent()
content.title = "Notification Demo"
content.subtitle = "Demo"
content.body = "Notification on specific date!!"

let request = UNNotificationRequest(
    identifier: "identifier",
    content: content,
    trigger: trigger
)

答案 1 :(得分:0)

首先你必须将swift注册为

func scheduleLocal(sender: AnyObject) {
let notification = UILocalNotification()
notification.fireDate = // give the next first date
notification.alertBody = "this is your notification"
notification.alertAction = "be awesome!"
notification.soundName = UILocalNotificationDefaultSoundName
notification.repeatInterval = NSCalendarUnit.NSCalendarUnitMonth
notification.userInfo = ["CustomField1": "w00t"]
UIApplication.sharedApplication().scheduleLocalNotification(notification)

然后安排为

 private Map <String, Object> conditions=new HashMap <String, Object>();
    public void addCondition(String field, Object condition){
            conditions.put(field, condition);
        }
    public Object getFilter(String field){
            return conditions.get(field);
        }
    public Map<String,Object>getFilterMap(){        
            return conditions;
        }
相关问题