iphone实现本地通知(类似推送)

http://www.cocoachina.com/bbs/read.php?tid=47755&keyword=UILocalNotification

#import <Foundation/Foundation.h>
#import "PlayBill.h"

@interface LocalNotificationsManager : NSObject
//添加通知
+ (void)addLocalNotificationWithFireDate:(NSString *)datestr

                              activityId:(PlayBill *)playBill

                           activityTitle:(NSString *)title;


//移除某个通知
+ (BOOL)removeLocalNotificationWithActivityId:(NSString *)billId;
//获取所有通知
+ (NSArray*)getLocalNotifications;
//得到id为billid的通知
+ (BOOL)getNotification:(NSString *)billId;
@end

#import "LocalNotificationsManager.h"

@implementation LocalNotificationsManager
+ (void)addLocalNotificationWithFireDate:(NSString *)datestr
                              activityId:(PlayBill *)playBill
                           activityTitle:(NSString *)title
{
    //设定的时间
//    NSString *strdate = @"17:30:00";

    NSDateFormatter *formatterhour = [[NSDateFormatter alloc]init];
    [formatterhour setDateFormat:@"HH:mm:ss"];
    NSDate *date = [formatterhour dateFromString:playBill.begin];
    NSLog(@"date:%@",date);
    
    //发送通知
    UILocalNotification *notification=[[UILocalNotification alloc] init];
    if (notification!=nil) {
//        NSDate *now=[NSDate new];
        notification.fireDate=[date dateByAddingTimeInterval:-60];//一分钟前通知
        notification.repeatInterval = NSDayCalendarUnit;
        
        notification.timeZone=[NSTimeZone defaultTimeZone];
        notification.soundName= UILocalNotificationDefaultSoundName;//声音,可以换成alarm.soundName = @"myMusic.caf"
        //去掉下面2行就不会弹出提示框
        notification.alertBody= [NSString stringWithFormat:@"《%@》马上就要开始了!",title];//提示信息 弹出提示框
        notification.alertAction = @"打开";  //提示框按钮

        DBLog(@"ctivityid,info:::%@,%@",playBill.name,playBill.playBillId);
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:playBill.playBillId, @"playBillId",playBill.name,@"name",playBill.begin,@"begin", nil];
        notification.userInfo = dic;
        
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }

    
}

+ (BOOL)removeLocalNotificationWithActivityId:(NSString *)billId //取消提醒

{
    
    UIApplication *application = [UIApplication sharedApplication];
    
    NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    
    for (UILocalNotification *obj in localNotifications) {
        
        NSString *playbillId = [obj.userInfo objectForKey:@"playBillId"];
                
        if ([playbillId isEqualToString:billId]) {
            
            [application cancelLocalNotification:obj];
            
            return YES;
            
        }
        
    }
    
    return NO;
    
}
//取出所有的通知
+ (NSArray*)getLocalNotifications
{    
    NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    DBLog(@"localNotification:%@",localNotifications);
    return localNotifications;
}
//取出某个通知是否存在 
+ (BOOL)getNotification:(NSString *)billId
{    
    NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    
    for (UILocalNotification *obj in localNotifications) {
        
        NSString *playbillId = [obj.userInfo objectForKey:@"playBillId"];
        
        if ([playbillId isEqualToString:billId]) {
            return YES;
            
        }
        
    }
    
    return NO;
}
@end

微笑

当你是从通知中进入应用中时,就要在appdelegate中写:(为了把通知中心中的通知取消掉)

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    //取消通知
//    notification.alertBody = @"";
    [[UIApplication sharedApplication] cancelLocalNotification:notification];
    //取消后再添加
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    
}

取消所有的通知:[[UIApplication sharedApplication] cancelAllLocalNotifications];

当有新版本更新时,以前的通知都还存在,这个时候要在app第一次打开的时候把以前所有的通知取消掉。

 //取消所有通知
    BOOL firstnotification = [[[NSUserDefaults standardUserDefaults] objectForKey:@"firstnotification"] boolValue];
    if (!firstnotification) {
        NSString *number = [NSString stringWithFormat:@"%d", YES];
        [[NSUserDefaults standardUserDefaults] setObject:number forKey:@"bHasLoad"];
        [[NSUserDefaults standardUserDefaults] synchronize];
		//do something you want when install
        
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
	}



你可能感兴趣的:(iphone实现本地通知(类似推送))