Adding text field in Remote Notification, iOS 8
Asked Answered
S

4

26

i want to add facility in my iOS application that user can reply to the my message directly from the push notification. Like iMessage app is doing it in iOS 8. I am able to add buttons to push notification but did not found any guide on how to add text field. Any Help Please. Thanks in advance.

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

I know how to add action(buttons) but how to add a text field or text view for input

enter image description here

Slake answered 15/9, 2014 at 5:15 Comment(0)
T
15

At the time of asking, this was not possible using the provided API. UIUserNotificationAction objects only represented additional buttons you can add, but you cannot input text directly. For now, you should add a "Reply" button, and on tap, opens the app with the keyboard visible so users can reply.

With iOS 9 SDK, this is possible by setting UIUserNotificationActionBehaviorTextInput as the behavior of the notification action. Make sure to read the documentation for more information.

See this answer for an example how to achieve this in Swift.

Trout answered 26/9, 2014 at 15:46 Comment(1)
to check some code check the answer below https://mcmap.net/q/521288/-adding-text-field-in-remote-notification-ios-8Slake
C
11

You cannot do this in iOS 8 or below but the same is possible with iOS 9, I have written a blog post regarding the same. It is pretty simple,

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

I was able to get it like below,

enter image description here

Clemons answered 3/8, 2015 at 18:33 Comment(12)
Any idea, how to transfer user input when the push pressed? Like in iMessage app. And do you have a bug with black premanent keyboard, when iPhone locked by a password, and you try to answer push on lock screen and you pressed it?Mckay
@RomanTruba I am not getting your question properly, could you please be more clear. You want to transfer user input to where and when?Clemons
See example from iMessage app: when you see a banner with a message, pull it to reply, then enter some text, and then press at banner, not a "send" button. You will see text you entered in the input field.Mckay
I am able to achieve this only when the app is in background, how to execute this to sen message when App in not in memory. I am trying to send an XMPP Messages to the sender who send PN.Oralla
@VickyDhas You should add the category key in your push payload. In my example it should be sent as "INVITE_CATEGORY" in the json, like {"category":"INVITE_CATEGORY","message":"","alert":"",,........}Clemons
I've done this , but one problem is when the app is in background I can do whatever I want to send XMPP Message but when the App is out of memory, nothing happens. Can you help how to achieve like whatsapp sending message when even not active in memory or not activated to send the reply from notification.Oralla
I am not sure of PN implementation, but it should work like normal apps regardless of being killed or in background state. The category has always worked for me in interactive notifications, while I was testing with PN's even when app was killed but not sure what is causing your problem.Clemons
@RomanTruba Do you figure out how to achieve the same behaviour iMessage has?Beneficence
@Beneficence nope. As I understood, there are no such possibility, because when you press on the notification, you can only operate with application:didReceiveRemoteNotification:Mckay
Yes you can in the remote notification payload add "category":"INVITE_CATEGORY" as a key.Clemons
@satheeshwaran.Can i add buttons "Accept","Reject",inputTextField,"Send" in notification.?Filigreed
@AvijitNagare Yes you can try but I don't think it is good user experience.Clemons
R
0

Swift 4.2, 4.0+ of satheeshwaran answer.

//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)
Redletter answered 18/7, 2018 at 7:15 Comment(1)
'UIMutableUserNotificationAction' was deprecated in iOS 10.0: Use UserNotifications Framework's UNNotificationActionFilia
A
0

The existing answers are old and use deprecated frameworks.

Here's how to achieve this with the latest User Notifications framework (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()
}

Result:

enter image description here

Docs: Declare your actionable notification types

Academicism answered 12/4, 2023 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.