玩转 iOS 开发:iOS 10 新特性《Notification Content Extensions》

栏目: IOS · 发布时间: 6年前

内容简介:玩转 iOS 开发:iOS 10 新特性《Notification Content Extensions》

文章分享至我的个人技术博客: https://cainrun.github.io/14974988224036.html

Notification Content Extensions

在之前我们就讲过苹果爸爸在 iOS 10 推出了一个新的 UserNotification 框架, 但苹果爸爸的野心不小, 不单单推出框架那么简单, 而且连 Extension 都给你搞了一个, 哼哼, 我看还有谁~

这里所演示的项目是和之前一样的, 如果找不到的盆友可以到这里去看看.

添加Notification Extension

回到我们的项目, 拷贝一份新的, 然后添加 Notification Extension :

玩转 iOS 开发:iOS 10 新特性《Notification Content Extensions》
1
玩转 iOS 开发:iOS 10 新特性《Notification Content Extensions》
2
玩转 iOS 开发:iOS 10 新特性《Notification Content Extensions》
3

配置Notification Extension Info

添加完 Extension 之后, 我们需要来配置一下 Info.plist 文件, 这里我们要添加点东西:

  • UNNotificationExtensionDefaultContentHidden
    • 设置成为YES
  • UNNotificationExtensionCategory
    • 和我们之前项目的 AppDelegate.m 文件所添加的 Category 保持一致, 我这里是 reminder
  • UNNotificationExtensionInitialContentSizeRatio
    • 这个东东我改为0.5

修改完成后的结果:

玩转 iOS 开发:iOS 10 新特性《Notification Content Extensions》
4

来通知的效果:

玩转 iOS 开发:iOS 10 新特性《Notification Content Extensions》
5

自定义一个小方法

上面的东西都搞定了之后, 那么接下来就是要自定义一个小方法:

- (void)addShakeAnimation {

    CAKeyframeAnimation *frameAnimation = [CAKeyframeAnimation animation];

    frameAnimation.keyPath        = @"transform.translation.x";
    frameAnimation.duration       = 1;
    frameAnimation.repeatCount    = MAXFLOAT;
    frameAnimation.values         = @[@-20.0, @20.0, @-20.0, @20.0, @-10.0, @10.0, @-5.0, @5.0, @0.0];
    frameAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    [self.view.layer addAnimation:frameAnimation
                           forKey:@"shake"];
}

- (void)removeShakeAnimation {

    [self.view.layer removeAnimationForKey:@"shake"];
}
  • addShakeAnimation添加摇摆动画的方法

  • removeShakeAnimation删除摇摆动画的方法

效果:

玩转 iOS 开发:iOS 10 新特性《Notification Content Extensions》
6

- didReceiveNotificationResponse:completionHandler:方法

Notification Extension 里, 有这么一个方法叫做: - didReceiveNotificationResponse:completionHandler: , 这个方法可以获取通知里的 Action 事件, 我这里是这么写的:

- (void)didReceiveNotificationResponse:(UNNotificationResponse *)response
                     completionHandler:(void (^)(UNNotificationContentExtensionResponseOption))completion {

    if ([response.actionIdentifier isEqualToString:@"cancel"]) {

        UNNotificationRequest *request = response.notification.request;

        NSArray *identifiers = @[request.identifier];

        // 根据标识符删除等待通知
        [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:identifiers];

        // 根据标识符删除发送通知
        [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:identifiers];

        self.label.text = @"点击了取消按钮";

        // 删除动画效果
        [self removeShakeAnimation];

        // 不隐藏通知页面
        completion(UNNotificationContentExtensionResponseOptionDoNotDismiss);

    } else {

        // 隐藏通知页面
        completion(UNNotificationContentExtensionResponseOptionDismiss);
    }
}

如果不懂这个动画应用的话, 我在度娘里看到了一篇文章, 可以来看看 IOS 核心动画之CAKeyframeAnimation

整体的运行效果:

玩转 iOS 开发:iOS 10 新特性《Notification Content Extensions》
7

总结

文章里只是简单的讲解, 如果还想了解更多的话, 可以自行去查看WWDC 2016的视频讲解.

工程地址

项目地址: https://github.com/CainRun/iOS-10-Characteristic/tree/master/6.Notification%20Content%20Extension

最后

码字很费脑, 看官赏点饭钱可好
玩转 iOS 开发:iOS 10 新特性《Notification Content Extensions》
微信
玩转 iOS 开发:iOS 10 新特性《Notification Content Extensions》
支付宝

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Hacking Growth

Hacking Growth

Sean Ellis、Morgan Brown / Crown Business / 2017-4-25 / USD 29.00

The definitive playbook by the pioneers of Growth Hacking, one of the hottest business methodologies in Silicon Valley and beyond. It seems hard to believe today, but there was a time when Airbnb w......一起来看看 《Hacking Growth》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具