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

问题描述 投票:0回答:4

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

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

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

enter image description here

objective-c iphone cocoa-touch push-notification ios8
4个回答
15
投票

在询问时,使用提供的 API 无法做到这一点。

UIUserNotificationAction
对象仅表示您可以添加的其他按钮,但您不能直接输入文本。现在,您应该添加一个“回复”按钮,并在点击时打开带有可见键盘的应用程序,以便用户可以回复。

对于 iOS 9 SDK,这可以通过将

UIUserNotificationActionBehaviorTextInput
设置为通知操作的
behavior
来实现。 请务必阅读文档以获取更多信息。

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


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>))

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


0
投票

Swift
4.2, 4.0+ satheeshwaran 答案。

//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)

0
投票

现有的答案是旧的并且使用已弃用的框架。

以下是如何使用最新的 UNNotificationCenter 框架(iOS 10.0+)实现此目的。

// AppDelegate.swift

static let sUserNotificationCenter: UNUserNotificationCenter = UNUserNotificationCenter.current()

// Notification action IDs
static let sAcceptActionID: String = "ACCEPT_ACTION"
static let sTentativeActionID: String = "TENTATIVE_ACTION"
static let sDeclineActionID: String = "DECLINE_ACTION"
static let sOtherResponseActionID: String = "OTHER_RESPONSE_ACTION"
    
// Notification category IDs
static let sMeetingInviteID: String = "MEETING_INVITE"

func RegisterNotificationCategories() {
        
    // Specify the actions (buttons) to a notification
    let meeting_accept: UNNotificationAction = UNNotificationAction(identifier: AppDelegate.sAcceptActionID, title: "ACCEPT")
    let meeting_tentative: UNNotificationAction = UNNotificationAction(identifier: AppDelegate.sTentativeActionID, title: "TENTATIVE")
    let meeting_decline: UNNotificationAction = UNNotificationAction(identifier: AppDelegate.sDeclineActionID, title: "DECLINE")
        
    // An action button which accepts a text from user.
    let other_response: UNTextInputNotificationAction = UNTextInputNotificationAction(identifier: AppDelegate.sOtherResponseActionID, title: "OTHER RESPONSE", textInputButtonTitle: "Enter", textInputPlaceholder: "placeholder")
        
    // Create the notification category object
    let meeting_invite: UNNotificationCategory = UNNotificationCategory(identifier: AppDelegate.sMeetingInviteID, actions: [meeting_accept, meeting_tentative, meeting_decline, other_response], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: "Preview")
        
    // Register the notification category
    AppDelegate.sUserNotificationCenter.setNotificationCategories([meeting_invite])
}

// Log(_:) is my own custom logger function. 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive pResponse: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        
    // obtain user's action on the notification.
    let user_action: String = pResponse.actionIdentifier
    Log("pResponse.actionIdentifier = " + user_action)
        
    if(user_action == TWAppDelegate.sOtherResponseActionID) {
            
        // If user has given a text response, the received response object will be of type
        // UNTextInputNotificationResponse, which has the user's text within
            
        let text_response: UNTextInputNotificationResponse = pResponse as! UNTextInputNotificationResponse
            
        Log(String(format: "User's text response = %@", text_response.userText))
    }
        
    // Call completion handler to let system know that processing is complete.
    completionHandler()
}

结果:

文档:声明您的可操作通知类型

© www.soinside.com 2019 - 2024. All rights reserved.