Customize local notification in objective-c

1k Views Asked by At

How to create this type of local notification in objective-c.

1

There are 1 best solutions below

1
On

Actually if you are setting up a local notification and you're just interested in having an image show up in the notification itself, you don't have to bother with NotificationsUI.framework

UNMutableNotificationContent *content = [UNMutableNotificationContent new];
    content.title = @"Title";
    content.body = @"Body";
    content.sound = [UNNotificationSound defaultSound];
    NSURL *imageURL = [NSURL URLWithString:@"file:/some/path/in/app/image.png"];
    NSError *error;
    UNNotificationAttachment *icon = [UNNotificationAttachment attachmentWithIdentifier:@"image" URL:imageURL options:nil error:&error];
    if (error)
    {
        NSLog(@"error while storing image attachment in notification: %@", error);
    }
    if (icon)
    {
        content.attachments = @[icon];
    }

Then when the notification appears, the image will show up on the right-hand side of the notification banner like it does for Messages notifications. And you don't have to jump through all of the hoops of setting up a content extension with a categoryIdentifier, etc.