我们更改通知应用程序iOS的内容

你好

在本文中,我想分享一下我的经验,即使用一个不太明显的选项(至少对我来说)来更改应用程序的通知文本,并通过UNUserNotificationCenter发送其他数据。

我希望本文对在Swift上进行iOS编程的初学者有用。 假定您至少具有一些使用Swift for iOS进行编程的经验。 我使用了Swift 5和Xcode 10.2.1。 因此,让我们开始吧。


挑战赛


我有一个显示用户通知的应用程序。 通知由正文和背景组成。

因此,其想法是首先仅向用户显示主要文本,如果用户想知道辅助文本,则必须单击通知中的“显示”按钮。

我使用以下方法显示通知(以下所有说明):

1.首先,我们必须定义一个UNUserNotificationCenter实例:

class NotificationService: NSObject, UNUserNotificationCenterDelegate { let notificationCenter = UNUserNotificationCenter.current() } 


2.接下来,我们声明一个将用于发送通知的方法:
我的通知请求方法
 // 1 //   ,      Tuple    . // showBody -     Bool,           . // withAction -     Bool,       ,   ""  . // atDate date -    Date,      ,      . func showNotification(with item: (String, String), showBody: Bool, withAction: Bool, atDate date: Date) { // 2 //    let content = UNMutableNotificationContent() // 3 //     let userActionsIdentifier = “showMe” // 4 //      . content.title = item.0 // 5 //    ,      false,  ,       . if showBody { content.body = item.1 } // 6 //     -    notificationCenter userInfo      . content.userInfo = [item.0: item.1] content.sound = UNNotificationSound.default // 7 //        withAction  true,      "Show me". if withAction { content.categoryIdentifier = userActionsIdentifier } // 8 //    ,        . let notificationID = item.0 // 9 //      . //       ,      . var dc = DateComponents() dc.hour = Calendar.current.component(.hour, from: date) dc.minute = Calendar.current.component(.minute, from: date) dc.second = Calendar.current.component(.second, from: date) // 10 //   .     ,       UNCalendarNotificationTrigger. let trigger = UNCalendarNotificationTrigger(dateMatching: dc, repeats: false) // 11 //        ,   . let request = UNNotificationRequest(identifier: notificationID, content: content, trigger: trigger) // 12 //     notificationCenter,     . notificationCenter.add(request) { (error) in error == nil ? print(“notifacation request was added at “, trigger.nextTriggerDate()!) : print(error.debugDescription) } // 13 //    .            .      . let action = UNNotificationAction(identifier: “showMe”, title: “Show me”, options: []) // 14 //   . let category = UNNotificationCategory(identifier:userActionsIdentifier, actions: [action], intentIdentifiers: [], options: []) // 15 //      notificationCenter. notificationCenter.setNotificationCategories([category]) } 



调用此方法时,我们将在设置时间看到一条通知。
如果用户将通知下拉,则将显示“向我显示”按钮,然后用户必须单击此按钮以查看背景文本,为此,我们必须使用UNUserNotificationCenterDelegate委托方法:

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { … } 


因此,我在此委托方法中使用的代码如下:

我的委托方法
 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print(“didReceive response”) // 1 //   switch     switch response.actionIdentifier { case “showMe”: print(“showMe action”) // 2 //      userInfo,      showNotification   6,      String. let mainText = response.notification.request.content.userInfo.keys.first! as! String // 3 //       userInfo,      showNotification   6,       String. let subText = response.notification.request.content.userInfo.values.first! as! String // 4 //       showNotificationlet,    : // showBody    true,  withAction  false (       ). self.showNotification(with: (mainText, subText), showBody: true, withAction: false, atDate: Date(timeInterval: 3, since: Date())) default: print(“defaul action”) } completionHandler() } 



这是它的样子:



在项目内部(您可以从下面的链接下载),还有更多代码,但是本文描述了该思想的实质。

谢谢你

链接到项目

Source: https://habr.com/ru/post/zh-CN451454/


All Articles