iOS10全新推送功能的实现


从iOS8.0开始推送功能的实现在不断改变,功能也在不断增加,iOS10又出来了一个推送插件的开发(见最后图),废话不多说直接上代码:

在开始之前需要打开一个推送开关,不然无法获取deviceToken,老项目或者出现deviceToken无效的情况:如图:



打开后会生成entitlements文件,需要有APS Environment


或许还应该打开这个


#import <UserNotifications/UserNotifications.h>


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    /* APP未启动,点击推送消息的情况下 iOS10遗弃UIApplicationLaunchOptionsLocalNotificationKey,使用代理UNUserNotificationCenterDelegate方法didReceiveNotificationResponse:withCompletionHandler:获取本地推送
     */
//    NSDictionary *localUserInfo = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
//    if (localUserInfo) {
//        NSLog(@"localUserInfo:%@",localUserInfo);
//        //APP未启动,点击推送消息
//    }
    NSDictionary *remoteUserInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
    if (remoteUserInfo) {
        NSLog(@"remoteUserInfo:%@",remoteUserInfo);
        //APP未启动,点击推送消息,iOS10下还是跟以前一样在此获取
    }
    [self registerNotification];
    return YES;
}

注册推送方法的改变:

新增库 #import <UserNotifications/UserNotifications.h>  推送单列UNUserNotificationCenter 等API


- (void)registerNotification{
    /*
     identifier:行为标识符,用于调用代理方法时识别是哪种行为。
     title:行为名称。
     UIUserNotificationActivationMode:即行为是否打开APP。
     authenticationRequired:是否需要解锁。
     destructive:这个决定按钮显示颜色,YES的话按钮会是红色。
     behavior:点击按钮文字输入,是否弹出键盘
     */
    UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"action1" title:@"策略1行为1" options:UNNotificationActionOptionForeground];
    /*iOS9实现方法
    UIMutableUserNotificationAction * action1 = [[UIMutableUserNotification
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值