iOS 本地推送

1.

创建本地推送

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建一个本地推送
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    
    //设置10s后
    NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10];
    if (notification != nil)
    {
        //设置推送时间
        notification.fireDate = pushDate;
        //设置时区
        notification.timeZone = [NSTimeZone defaultTimeZone];
        //设置重复间隔
        notification.repeatInterval = kCFCalendarUnitMinute;
        //设置推送声音
        notification.soundName = UILocalNotificationDefaultSoundName;
        //推送内容
        notification.alertBody = @"推送内容";
        //推送标题
        notification.alertTitle = @"本地推送";
        //显示在app上的红色圈子中的数字
        notification.applicationIconBadgeNumber = 1;
        //设置userInfo ,方便在需要的撤销的时候使用
        NSDictionary *info = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
        notification.userInfo = info;
        //添加推送到application
        UIApplication *app = [UIApplication sharedApplication];
        [app scheduleLocalNotification:notification];
    }
}

2.

接受本地推送

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"iWeibo" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
    //图标上的数字减1
    application.applicationIconBadgeNumber -= 1;
}

//因为iOS8回接受不到推送,因此需要做些处理

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //处理iOS8本地推送不能收到
    float sysVersion=[[UIDevice currentDevice]systemVersion].floatValue;
    if (sysVersion>=8.0) {
        UIUserNotificationType type=UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound;
        UIUserNotificationSettings *setting=[UIUserNotificationSettings settingsForTypes:type categories:nil];
        [[UIApplication sharedApplication]registerUserNotificationSettings:setting];
    }
    
    return YES;
}



The end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值