Oc 本地推送

为加强应用程序与用户的黏度,可以不管应用在前台还是后台运行,对用户进行提醒,提醒用户即将要做的事情
作为iOS开发者,如何在iOS应用中实现该功能?iOS提供了哪几种推送方式?
iOS使用NSNotificationCenter实现观察者模式,允许应用的不同对象之间以松耦合的方式进行通信
1.iOS提供了本地通知,可以定时向用户发送提示消息
2.iOS使用远程通知,可以将服务器数据发送给iOS客户端
10.4.1实例-本地通知
我们将用一个demo来实现一个简单的本地通知

首先设置UIApplication的全局变量和初始化

@interface ViewController ()
{
    UIApplication *app;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    app = [UIApplication sharedApplication];
    
}

然后在switch 按钮方法里写

- (IBAction)turn:(UISwitch *)sender
{
    if (sender.on)
    {
        if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
        {
            [[UIApplication sharedApplication]registerUserNotificationSettings:
            [UIUserNotificationSettings settingsForTypes:
             UIUserNotificationTypeAlert |
             UIUserNotificationTypeBadge |
             UIUserNotificationTypeSound categories:nil]];
        }
        UILocalNotification *notification = [[UILocalNotification alloc]init];
        notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
        notification.timeZone = [NSTimeZone defaultTimeZone];
        notification.repeatInterval = kCFCalendarUnitDay;
        notification.soundName = @".mp3";
        notification.alertTitle = @"镇魂街";
        notification.alertBody = @"好几天没登陆了";
        notification.applicationIconBadgeNumber = 1;
        
        NSDictionary *info = @{@"lz":@"key"};
        notification.userInfo = info;
        
        [app scheduleLocalNotification:notification];
    }
    else
    {
        NSArray *localArray = [app scheduledLocalNotifications];
        if (localArray)
        {
            for (UILocalNotification *noti in localArray)
            {
                NSDictionary *dict = noti.userInfo;
                if (dict)
                {
                    // 如果找到要取消的通知
                    NSString *inKey = [dict objectForKey:@"lz"];
                    if ([inKey isEqualToString:@"lz"])
                    {
                        // 取消调度该通知
                        [app cancelLocalNotification:noti];  // ②
                    }
                }
            }
        }
    }
}

第一个判断是判断switch 在打开时和关闭时

在switch打开时的第二个判断是因为版本的问题是否能回应registerUserNotificationSettings方法

// 创建一个本地通知

UILocalNotification *notification = [[UILocalNotification alloc]init];

// 设置通知的触发时间

notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];

// 设置通知的时区

notification.timeZone = [NSTimeZone defaultTimeZone];

// 设置通知的重复发送的事件间隔

notification.repeatInterval = kCFCalendarUnitMinute;

// 设置通知的声音

notification.soundName = @".mp3";

//通知标题

notification.alertTitle=@"镇魂街";

notification.alertBody = @"好几天没登陆了";

// 设置显示在应用程序上红色徽标中的数字

notification.applicationIconBadgeNumber = 1;

// 设置userinfo,用于携带额外的附加信息。

NSDictionary *info = @{@"lz": @"key"};

notification.userInfo = info;

// 调度通知

[app scheduleLocalNotification:notification];

else 里

// 获取所有处于调度中本地通知数组

NSArray *localArray = [app scheduledLocalNotifications];

用遍历方法遍历通知数组

我们还要在AppDelegate里写

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    application.applicationIconBadgeNumber = 0;
    
    [[[UIAlertView alloc] initWithTitle:@"收到通知"
                                message:notification.alertBody
                               delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    application.applicationIconBadgeNumber = 0;
}

你可能感兴趣的:(Oc 本地推送)