在远程通知,iOS 8中添加文本字段

时间:2014-09-15 05:15:16

标签: objective-c iphone cocoa-touch push-notification ios8

我想在我的iOS应用程序中添加工具,用户可以直接从推送通知回复我的消息。就像iMessage应用程序在iOS 8中一样。我可以添加按钮来推送通知,但没有找到任何有关如何添加文本字段的指南。 请帮忙。提前谢谢。

-------------------------------------------- ----------------------------------------------

我知道如何添加操作(按钮),但如何为输入添加文本字段或文本视图

enter image description here

3 个答案:

答案 0 :(得分:15)

在询问时,使用提供的API无法做到这一点。 UIUserNotificationAction个对象仅代表您可以添加的其他按钮,但您无法直接输入文本。现在,您应该添加一个“回复”按钮,然后点按,打开应用程序,键盘可见,以便用户回复。

使用iOS 9 SDK,可以将UIUserNotificationActionBehaviorTextInput设置为通知操作的behavior。 请务必阅读文档以获取更多信息。

有关如何在Swift中实现此目的的示例,请参阅this答案。

答案 1 :(得分:11)

你无法在iOS 8或更低版本中执行此操作,但iOS 9也可以这样做,我写了blog post相同的内容。这很简单,

 //creating the inline reply notification action
   let replyAction = UIMutableUserNotificationAction()
   replyAction.title = "Say Something"
   replyAction.identifier = "inline-reply"
   replyAction.activationMode = .Background
   replyAction.authenticationRequired = false
   replyAction.behavior = .TextInput

 //creating a category
   let notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
   notificationCategory.identifier = "INVITE_CATEGORY"
   notificationCategory .setActions([replyAction], forContext: UIUserNotificationActionContext.Default)

 //registerting for the notification.
      application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:[ UIUserNotificationType.Sound, UIUserNotificationType.Alert,
            UIUserNotificationType.Badge], categories: NSSet(array:[notificationCategory]) as? Set<UIUserNotificationCategory>))

我能够像下面那样得到它,

enter image description here

答案 2 :(得分:-1)

Swift的{​​{3}}答案的 4.2,4.0 +

//creating the inline reply notification action
let replyAction = UIMutableUserNotificationAction()
replyAction.title = "Say Something"
replyAction.identifier = "inline-reply"
replyAction.activationMode = .background
replyAction.isAuthenticationRequired = false
replyAction.behavior = .textInput

//creating a category
let notificationCategory = UIMutableUserNotificationCategory()
notificationCategory.identifier = "INVITE_CATEGORY"
notificationCategory.setActions([replyAction], for: .default)

//registerting for the notification.
let categories = NSSet(array: [notificationCategory]) as? Set<UIUserNotificationCategory>
let settings = UIUserNotificationSettings(types: [ .sound, .alert,.badge], categories: categories)
application.registerUserNotificationSettings(settings)