本地通知UILocalNotification

本地推送UILocalNotification

  • 獲取每天的9點的時間NSDate
    NSDate *date = [NSDate date];
    NSTimeZone *gmt = [NSTimeZone systemTimeZone];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    [gregorian setTimeZone:gmt];
    NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
    [components setHour:9];
    [components setMinute:0];
    [components setSecond: 0];
    NSDate *newDate = [gregorian dateFromComponents:components];
  • 增加一個本地推送

    notification.fireDate = newDate; 設置推送時間
    notification.timeZone=[NSTimeZone defaultTimeZone];設置時區(qū)
    notification.soundName = UILocalNotificationDefaultSoundName;推送聲音
    noti.applicationIconBadgeNumber = 1;顯示在icon上的紅色圈中的數(shù)子
    NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
    noti.userInfo = infoDic;設置userinfo 方便在之后需要撤銷的時候使用
    notification.repeatInterval = kCFCalendarUnitDay;設置重復間隔
    notification.alertBody = @"推送內(nèi)容";
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];添加推送到UIApplication

  • 程序運行時接收到本地推送消息
    -(void)application:(UIApplication )application didReceiveLocalNotification:(UILocalNotification)notification{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"接收到本地提醒 in app" message:notification.alertBody delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
    [alertView show];
    //這里,你就可以通過notification的useinfo,干一些你想做的事情了
    application.applicationIconBadgeNumber -= 1;
    }
  • 取消一個本地推送
    UIApplication *app = [UIApplication sharedApplication];
    //獲取本地推送數(shù)組
    NSArray *localArr = [app scheduledLocalNotifications];
    //聲明本地通知對象
    UILocalNotification *localNoti;
    if (localArr) {
    for (UILocalNotification *noti in localArr) {
    NSDictionary *dict = noti.userInfo;
    if (dict) {
    NSString *inKey = [dict objectForKey:@"key"];
    if ([inKey isEqualToString:key]) {
    if (localNoti){
    [localNoti release];
    localNoti = nil;
    }
    localNoti = [noti retain];
    break;
    }
    }
    }
    //判斷是否找到已經(jīng)存在的相同key的推送
    if (!localNoti) {
    //不存在 初始化
    localNoti = [[UILocalNotification alloc] init];
    }
    if (localNoti && !state) {
    //不推送 取消推送
    [app cancelLocalNotification:localNoti];
    [localNoti release];
    return;
    }
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容