-4

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

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Neeraj Sonaro
  • 346
  • 1
  • 16

1 Answers1

0

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.

Shahbaz Akram
  • 1,598
  • 3
  • 28
  • 45