iOS 本地推送 UILocalNotification

创建通知

UILocalNotification *notification = [[UILocalNotification alloc] init];
    if (notification)
    {
        NSDate *now = [NSDate new];
        notification.fireDate = [now dateByAddingTimeInterval:10]; //10秒后通知
        notification.repeatInterval=0; //重复次数,kCFCalendarUnitWeekday一周一次
        notification.timeZone = [NSTimeZone defaultTimeZone]; //设置时区
        notification.applicationIconBadgeNumber = 1; //应用的角标
        notification.soundName = UILocalNotificationDefaultSoundName; //声音,可以换成alarm.soundName = @"sound.wav"
        //去掉下面2行就不会弹出提示框
        notification.alertBody = @"通知内容"; //提示信息 弹出提示框
        notification.alertAction = @"打开"; //提示框按钮
        //notification.hasAction = NO; //是否显示额外的按钮,为no时alertAction消失
        NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
        //设置userinfo 方便在之后需要撤销的时候使用 也可以传递其他值,当通知触发时可以获取
        notification.userInfo = infoDict;
        
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }


推送的内容

//推送的内容
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification
{


    //这里,你就可以通过notification的useinfo,干一些你想做的事情了
    if ([[notification.userInfo objectForKey:@"key"] isEqualToString:@"value"])
    {
           UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"系统提示" message:@"你的系统存在严重威胁" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil,nil];
           [alert show];
        
    }
    application.applicationIconBadgeNumber = 0; //移除角标
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    //不通过推送 通过应用图标打开应用 移除角标
    application.applicationIconBadgeNumber = 0;
}

取消通知

//获取当前所有的本地通知
    NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];
    if (!notificaitons || notificaitons.count <= 0)
    {
        return;
    }
    //取消一个特定的通知
    for (UILocalNotification *notify in notificaitons)
    {
        if ([[notify.userInfo objectForKey:@"key"] isEqualToString:@"value"])
        {
            [[UIApplication sharedApplication] cancelLocalNotification:notify];
            break;
        }
    }
    
//    //取消所有的本地通知
//    [[UIApplication sharedApplication] cancelAllLocalNotifications];



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值