在 objective-c 中自定义本地通知



如何在objective-c中创建这种类型的本地通知。

实际上,如果您正在设置本地通知,并且您只是对在通知本身中显示图像感兴趣,则不必为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];
}

然后,当通知出现时,图像将显示在通知横幅的右侧,就像消息通知一样。而且,您不必跳过使用类别标识符等设置内容扩展的所有箍。

最新更新