IOS本地推送

第一步:建立本地推送
// 建立一個本地推送
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
//設置10秒以後
NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10];
if (notification != nil) {
    // 設置推送時間
    notification.fireDate = pushDate;
    // 設置時區
    notification.timeZone = [NSTimeZone defaultTimeZone];
    // 設置重複間隔
    notification.repeatInterval = kCFCalendarUnitDay;
    // 推送聲音
    notification.soundName = UILocalNotificationDefaultSoundName;
    // 推送內容
    notification.alertBody = @"推送內容";
    //顯示在icon上的紅色圈中的數子
    notification.applicationIconBadgeNumber = 1;
    //設置userinfo 方便在以後須要撤銷的時候使用
    NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"];
    notification.userInfo = info;
    //添加推送到UIApplication       
    UIApplication *app = [UIApplication sharedApplication];
    [app scheduleLocalNotification:notification]; 
   
}

第二步:接收本地推送
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iWeibo" message:notification.alertBody delegate:nil cancelButtonTitle:@"肯定" otherButtonTitles:nil];
    [alert show];
    // 圖標上的數字減1
    application.applicationIconBadgeNumber -= 1;
}

第三步:解除本地推送
// 得到 UIApplication
UIApplication *app = [UIApplication sharedApplication];
//獲取本地推送數組
NSArray *localArray = [app scheduledLocalNotifications];
//聲明本地通知對象
UILocalNotification *localNotification;
if (localArray) {
    for (UILocalNotification *noti in localArray) {
        NSDictionary *dict = noti.userInfo;
        if (dict) {
            NSString *inKey = [dict objectForKey:@"key"];
            if ([inKey isEqualToString:@"對應的key值"]) {
                if (localNotification){
                    [localNotification release];
                    localNotification = nil;
                }
                localNotification = [noti retain];
                break;
            }
        }
    }
   
    //判斷是否找到已經存在的相同key的推送
    if (!localNotification) {
        //不存在初始化
        localNotification = [[UILocalNotification alloc] init];
    }
   
    if (localNotification) {
        //不推送 取消推送
        [app cancelLocalNotification:localNotification];
        [localNotification release];
        return;
    }
}第一步:建立本地推送
// 建立一個本地推送
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
//設置10秒以後
NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10];
if (notification != nil) {
    // 設置推送時間
    notification.fireDate = pushDate;
    // 設置時區
    notification.timeZone = [NSTimeZone defaultTimeZone];
    // 設置重複間隔
    notification.repeatInterval = kCFCalendarUnitDay;
    // 推送聲音
    notification.soundName = UILocalNotificationDefaultSoundName;
    // 推送內容
    notification.alertBody = @"推送內容";
    //顯示在icon上的紅色圈中的數子
    notification.applicationIconBadgeNumber = 1;
    //設置userinfo 方便在以後須要撤銷的時候使用
    NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"];
    notification.userInfo = info;
    //添加推送到UIApplication       
    UIApplication *app = [UIApplication sharedApplication];
    [app scheduleLocalNotification:notification]; 
   
}

第二步:接收本地推送
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iWeibo" message:notification.alertBody delegate:nil cancelButtonTitle:@"肯定" otherButtonTitles:nil];
    [alert show];
    // 圖標上的數字減1
    application.applicationIconBadgeNumber -= 1;
}

第三步:解除本地推送
// 得到 UIApplication
UIApplication *app = [UIApplication sharedApplication];
//獲取本地推送數組
NSArray *localArray = [app scheduledLocalNotifications];
//聲明本地通知對象
UILocalNotification *localNotification;
if (localArray) {
    for (UILocalNotification *noti in localArray) {
        NSDictionary *dict = noti.userInfo;
        if (dict) {
            NSString *inKey = [dict objectForKey:@"key"];
            if ([inKey isEqualToString:@"對應的key值"]) {
                if (localNotification){
                    [localNotification release];
                    localNotification = nil;
                }
                localNotification = [noti retain];
                break;
            }
        }
    }
   
    //判斷是否找到已經存在的相同key的推送
    if (!localNotification) {
        //不存在初始化
        localNotification = [[UILocalNotification alloc] init];
    }
   
    if (localNotification) {
        //不推送 取消推送
        [app cancelLocalNotification:localNotification];
        [localNotification release];
        return;
    }
}數組

相關文章
相關標籤/搜索