ios本地推送

来源:互联网 发布:杭州阿里妈妈软件 编辑:程序博客网 时间:2024/05/16 02:10

在这之前,当然还要在didFinishLaunchingWithOptions注册通知。

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

//下面是发送通知

    

    NSArray *arr = [UIApplicationsharedApplication].scheduledLocalNotifications;

    for (UILocalNotification *notifyin arr) {

        // 通过category判断,或者可以通过notify.userInfo来判断

        if ([notify.categoryisEqualToString:@"34234234"]) {

            [[UIApplicationsharedApplication] cancelLocalNotification:notify];

            NSLog(@"isremove");

        }

    }

    UILocalNotification *localNoti = [[UILocalNotificationalloc] init];

    localNoti.fireDate = [NSDatedateWithTimeIntervalSinceNow:10];

    localNoti.alertTitle =@"标题";

    localNoti.alertBody =@"哈哈,本地推送";

    localNoti.applicationIconBadgeNumber =1;

    localNoti.soundName =UILocalNotificationDefaultSoundName;

    localNoti.category =@"34234234";

    // 还可以携带信息,这样可以处理收到通知,做什么处理

    localNoti.userInfo =@{@"selectIndex" :@(2)};

    [[UIApplicationsharedApplicationscheduleLocalNotification:localNoti];


    // 立即发

    [[UIApplicationsharedApplication] presentLocalNotificationNow:localNoti];