iosswiftunusernotificationcenterunnotificationrequest

Insert text in other apps automatically - swift4


I have a simple application, which can send user UNNotification periodically. I want to insert/paste some strings into the application which is behind of the notification.

For example, if the user is on the Note application on his iPhone, when he received my notification, I want to insert some texts in the opened Note.

Is this possible?

here is my code for sending the notification:

    let notification = UNMutableNotificationContent()
    notification.sound = UNNotificationSound.default()
    notification.subtitle = Message
    notification.body = "Expand this notification for more options"
    notification.categoryIdentifier = "Event"
    let category = UNNotificationCategory(identifier: "Event",
                                          actions: [],
                                          intentIdentifiers: [],
                                          options: [])
    let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: GenerateFireDate(), repeats: false)
    let request = UNNotificationRequest(identifier: "Event",
                                        content: notification,
                                        trigger: notificationTrigger)
    UNUserNotificationCenter.current().setNotificationCategories([category])
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

Update

I know I have a straightforward solution, which is to open the notification and Copy text in the UIPasteboard. But I am looking for an alternative way, which is more convenient rather than open-and-copy solution.


Solution

  • This is not possible. Consider that situation where any application would be able to paste in text using anywhere – it would not exactly make of a good experience.

    A way around it would be that you instruct the user to tap the notification to open your app. Then you could add text to clipboard. Read more about UIPasteboard in its documentation.