Search code examples
iosswiftvoipuilocalnotificationpushkit

How to Change UILocalNotification Text after it's shown in notification center?


In one of my app, I want to show the local notification and once it's shown in notification center, I want to updated it's content.

Is it possible in Swift ?

Thanks,

HP.


Solution

  • Here's the solution for other visitors:

    https://developer.sinnerschrader-mobile.com/ios-how-to-remove-a-notification-programmatically-from-ios-notification-center/582/

    Using NSKeyedArchiver, I archived my old local notification to one path and when I wanted to remove it, I deleted it from that location.

    It's working fine.

    Edited :

    1)Cache UILocalNotificaiton using NSKeyedArchiver

    NSString *archivePath = [self cachePathWithKey:key];
    [NSKeyedArchiver archiveRootObject:notification toFile:archivePath];
    

    2) Show Notification

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    

    3) Remove Notification Whenever you want to remove it.

    NSString *archivePath = [self cachePathWithKey:key];
    
    UILocalNotification *cachedNotification = [self notificationForKey:key];
    if (cachedNotification == nil) {
        return NO;
    }
    
    [[UIApplication sharedApplication] cancelLocalNotification:cachedNotification];
    
    [[NSFileManager defaultManager] removeItemAtPath:archivePath error:nil];
    

    You can use this library also as I've used that one only.

    https://github.com/sinnerschrader-mobile/s2m-toolbox-ios

    Copy LocalNotificationHelper Folder in your project and User removeNotificationForKey and showNotification methods.

    Thanks,

    HP.