苹果系统的本地推送

现在开发远程推送一般都是第三方极光推送,这里介绍苹果原生本地推送方法 前台/后台/退出

在appDelegate中接收推送的方法

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

    //1. 判断是否有通知需要处理->程序启动时调用的方法,也就是程序之前被关闭了
    if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {

        //2. 获取本地通知
        UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];

        //3. 实现界面跳转
        [self switchTabBarWithNotification:notification];

        //程序被关闭后不能得知是否接收到任何消息
//        UILabel *label = [UILabel new];
//        label.text = [NSString stringWithFormat:@"%@", launchOptions];
//        label.frame = CGRectMake(0, 0, 375, 300);
//        label.numberOfLines = 0;
//        [self.window.rootViewController.view addSubview:label];
    }

  // 设置分类
    UIMutableUserNotificationCategory *category = [UIMutableUserNotificationCategory new];
    category.identifier = @"category";

    // 设置一个前台按钮
    UIMutableUserNotificationAction *action1 = [UIMutableUserNotificationAction new];
    action1.identifier = @"foreground";
    action1.activationMode = UIUserNotificationActivationModeForeground;
    action1.title = @"前台";

    // 设置一个后台按钮
    UIMutableUserNotificationAction *action2 = [UIMutableUserNotificationAction new];
    action2.identifier = @"background";
    action2.activationMode = UIUserNotificationActivationModeBackground;
    action2.title = @"后台";

    // 设置通知出现的下拉处理按钮
    [category setActions:@[action1, action2] forContext:UIUserNotificationActionContextDefault];

    NSSet *categorySet = [NSSet setWithObjects:category, nil];

    // 设置需要请求的权限
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:categorySet];

    // 注册用户权限设置
    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];


    return YES;
}
#pragma mark -这是处理UIMutableUserNotificationAction 的方法,根据ID
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
    if ([identifier isEqualToString:@"foreground"]) {
        NSLog(@"前台运行中");
    } else if ([identifier isEqualToString:@"background"]) {
        NSLog(@"后台运行中");
    }
    completionHandler();
}
#pragma mark 当应用程序收到本地通知点击的时候调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    //实现界面跳转
    [self switchTabBarWithNotification:notification];

}

- (void)switchTabBarWithNotification:(UILocalNotification *)notification
{
    //1. 判断当前程序的状态
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
        return;
    }

    //2. 获取根控制器
    UITabBarController *tabBarC =  (UITabBarController *)self.window.rootViewController;

    //3. 获取索引信息
    NSInteger index = [notification.userInfo[@"tabBarIndex"] integerValue];

    //4. 切换
    tabBarC.selectedIndex = index;
}

在控制器里创建推送,并发送推送的过程

- (void)touches
{

    //1. 创建本地通知
    UILocalNotification *notifi = [UILocalNotification new];

    //2. 设置属性
    //2.1 设置发送时间
    notifi.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

    //2.2 设置发送内容
    notifi.alertBody = @"呵呵";

    //2.3 设置声音
    // 只有真机才有声音.  如果真机是静音状态, 会变成震动提示
    notifi.soundName = UILocalNotificationDefaultSoundName;

    //2.4 设置角标
    notifi.applicationIconBadgeNumber = 10;

    //2.5 设置点击通知时要显示的启动图
    notifi.alertLaunchImage = @"UILaunchImageFile";

    #pragma mark 不常用属性
    //2.6 重复间隔 --> 最小为分钟 --> 如果设置了重复, 通知发送后不会从调度池中移除
    notifi.repeatInterval = NSCalendarUnitMinute;

    //2.7 重复时参考的日历
    // 中国农历: NSCalendarIdentifierChinese
    //notifi.repeatCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:@"NSCalendarIdentifierChinese"];

    // 根据用户设置自动更新 --> 这个属性一般不用设置, 跟随系统日历
    //notifi.repeatCalendar = [NSCalendar autoupdatingCurrentCalendar];

    //3. 添加到调度池中
    [[UIApplication sharedApplication] scheduleLocalNotification:notifi];

    //4. 移除调度池中的通知

    // 取消当前应用注册过的通知
    //[[UIApplication sharedApplication] cancelAllLocalNotifications];

    // 取消指定的通知
    //[[UIApplication sharedApplication] cancelLocalNotification:notifi];

    // 获取通知
    NSArray *notifis = [[UIApplication sharedApplication] scheduledLocalNotifications];
    NSLog(@"notifi: %@",notifis);
 //记住iOS8以后一定要在登录代理方法中验证权限

分享就到这里啦~~极光推送很简单,下载推送包,按照他们官网的步骤设置即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值