UILocalNotification本地推送

来源:互联网 发布:sopcast网络电视安卓版 编辑:程序博客网 时间:2024/06/06 23:59

//远程推送 –> 服务器(第三方 umeng JPush(极光)) –> APNS

本地推送

Demo1
//获取推送权限

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

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:10];NSLog(@"fireDate=%@",fireDate);localNotification.fireDate = fireDate;// 时区localNotification.timeZone = [NSTimeZone defaultTimeZone];// 设置重复的间隔localNotification.repeatInterval = kCFCalendarUnitSecond;// 通知内容localNotification.alertBody =  @"该起床了...";localNotification.applicationIconBadgeNumber = 1;// 通知被触发时播放的声音localNotification.soundName = UILocalNotificationDefaultSoundName;// 通知参数NSDictionary *userDict = [NSDictionary dictionaryWithObject:@“哈哈哈” forKey:@"key"];localNotification.userInfo = userDict;// ios8后,需要添加这个注册,才能得到授权if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {    UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];} // 执行通知注册[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

}

  • (void)application:(UIApplication )application didReceiveLocalNotification:(UILocalNotification )notification {
    NSLog(@”noti:%@”,notification);

    // 这里真实需要处理交互的地方
    // 获取通知所带的数据
    NSString *notMess = [notification.userInfo objectForKey:@”key”];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”本地通知(前台)”message:notMess
    delegate:nil
    cancelButtonTitle:@”OK”
    otherButtonTitles:nil];
    [alert show];

    // 更新显示的徽章个数
    NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
    badge–;
    badge = badge >= 0 ? badge : 0;
    [UIApplication sharedApplication].applicationIconBadgeNumber = badge;

    // 在不需要再推送时,可以取消推送
    //[HomeViewController cancelLocalNotificationWithKey:@”key”];
    }

Demo2

//本地推送

//远程推送 –> 服务器(第三方 umeng JPush(极光)) –> APNS

@interface ViewController ()

@end

@implementation ViewController

-(void)localNotification
{
UILocalNotification *local = [[UILocalNotification alloc] init];

//发送的时间local.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];//时区local.timeZone = [NSTimeZone defaultTimeZone];//设置发送的消息local.alertBody = @"哈哈哈";//设置应用程序的徽标local.applicationIconBadgeNumber = 1;//设置声音local.soundName = UILocalNotificationDefaultSoundName;//userInfolocal.userInfo = @{@"key":@"阿斯顿和父爱和发件客户卡拉好看哈克龙"};//获取推送的权限if([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]){    UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];}[[UIApplication sharedApplication] scheduleLocalNotification:local];//取消本地推送//[UIApplication sharedApplication] cancelLocalNotification:(nonnull UILocalNotification *)

}

0 0
原创粉丝点击