iOS推送(一):本地推送

以iOS8.0和iOS10.0两个版本来创建一个本地推送:


- (void)localPush
{
// iOS8.0下本地推送的创建方式
    if ([UIDevice currentDevice].systemVersion.floatValue < 10.0) {
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5.0];
        localNotification.alertBody = @"这是一条本地推送,这是推送主体!";
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        localNotification.applicationIconBadgeNumber += 1;
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

      // iOS10.0  
    }else {

        // 给推送一个标识符
        NSString *identifier = @"localPush.identifier";

        // 推送内容
        UNMutableNotificationContent *content = [UNMutableNotificationContent new];
        content.body = @"这是iOS10的本地推送,这是推送主体!";
        content.badge = @1;

     // 推送触发器   
//        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];

        NSDateComponents *components = [NSDateComponents new];
        components.second = 5.0f;
        UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];

        // 推送请求
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];

        // 把推送请求添加到推送中心
        [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            NSLog(@"%@", [error localizedDescription]);
        }];
    }
}

推送创建完毕后,那么我们便要考虑收到推送后会执行什么样的操作,这里主要介绍处理接收到推送的地方。

首先我们要先注册推送通知。8.0和10.0注册推送还是很不一样的。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 8.0系统注册推送
    if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil];
        [application registerUserNotificationSettings:settings];

    // iOS10.0注册推送  
    }else{

        [UNUserNotificationCenter currentNotificationCenter].delegate = self;

        [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (error != nil) {
                NSLog(@"%@", [error localizedDescription]);
            }
        }];

        [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            NSLog(@"%@", settings);
        }];
    }


// 如果是点击推送进入的App,那么lauchOptions会有一个UIApplicationLaunchOptionsLocalNotificationKey字段,如果这个字段不为空,代表是点击推送进入,并且可以获得推送的内容。注意这个key在iOS10以后也有值,不要和userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:这个方法重复了。所以要判断一下版本是否小于10.0。

    if (([[UIDevice currentDevice].systemVersion floatValue] < 10.0) && launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
      // ......在点击推送进入App后会执行的代码。  
}

    return YES;
}

iOS10以后,如果程序被销毁后(即程序退出后),收到推送,点击推送进入app,会调用该方法;在该方法里可以执行需要执行的代码,注意两点:
一.要调用一下completionHandler()
二.要在

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

中设置一下代理:

[UNUserNotificationCenter currentNotificationCenter].delegate = self
;
否则该代理方法不会执行。

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
    //......程序退出时收到推送执行的操作
    completionHandler();
}

iOS8.0以后,如果程序在前台,收到推送会调用该方法:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    //......程序在前台时收到推送执行的操作
}

iOS10.0以后,如果程序在前台,收到推送会调用该方法:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
    //......程序在前台时收到推送执行的操作
    completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值