如何设置首次设置通知后每秒触发的本地通知?

时间:2017-07-21 05:42:35

标签: ios swift swift3 localnotification unusernotificationcenter

我想要的是,我将当地通知设置为早上7点,并立即开火,通知会持续发出,直到用户不对通知执行任何操作或打开应用程序。

下面是第一次发送通知的代码

 let alarmNotification: UNMutableNotificationContent = UNMutableNotificationContent()
 alarmNotification.title = "Demo"
 alarmNotification.body = "Test"
 alarmNotification.categoryIdentifier = "myDemoCategory"

 let now = Date()
 let triggerWeekly = Calendar.current.dateComponents([.weekday,.hour,.minute], from: now)
 let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
 let request = UNNotificationRequest(identifier: "TestNotification\(now)", content: alarmNotification, trigger: trigger)

 UNUserNotificationCenter.current().add(request) {(error) in
     if let error = error {         
           print("Uh oh! We had an error: \(error)")
     }
 }

请建议您继续发送本地通知。

由于

1 个答案:

答案 0 :(得分:0)

在您的日期组件中添加秒数

List<RowData> rowDataValues = new ArrayList<>();

        List<CellData> headerValues = new ArrayList<>();
        headerValues.add(new CellData().setNote("ID")
            .setUserEnteredValue(new ExtendedValue()
                .setStringValue("#")).setUserEnteredFormat(myFormat));
                        .setStringValue("Environment")).setUserEnteredFormat(myFormat));

.........

        headerValues.add(new CellData()
            .setUserEnteredValue(new ExtendedValue()
                .setStringValue("Name")).setUserEnteredFormat(myFormat));
        RowData setHeaderValues = new RowData();
        setHeaderValues.setValues(headerValues);
        rowDataValues.add(setHeaderValues);

requests.add(new Request()
            .setUpdateCells(new UpdateCellsRequest()
                .setStart(new GridCoordinate()
                    .setSheetId(randomSheetId)
                    .setRowIndex(0)
                    .setColumnIndex(0))
                .setRows(rowDataValues)
                .setFields("*")));

        BatchUpdateSpreadsheetRequest body = new BatchUpdateSpreadsheetRequest().setRequests(requests);

        BatchUpdateSpreadsheetResponse response = service.spreadsheets().batchUpdate(spreadsheetId, body).execute();

通过递增秒来设置循环中每秒所需的通知数量。在iOS中,最大值为64。

点击通知时会关闭所有通知并重置以供下次使用。