ios APNS注册失败 本地push

Posted on 2019-04-26 11:35  陈小鑫  阅读(449)  评论(0编辑  收藏  举报

- (void)addLocalNotice:(NSString *)titlepush {

if (@available(iOS 10.0, *)) {

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

// 标题

content.title = titlepush;

content.subtitle = titlepush;

// 内容

content.body = titlepush;

// 声音

// 默认声音

// content.sound = [UNNotificationSound defaultSound];

// 添加自定义声音

content.sound = [UNNotificationSound soundNamed:@"Alert_ActivityGoalAttained_Salient_Haptic.caf"];

// 角标 (我这里测试的角标无效,暂时没找到原因)

content.badge = @1;

// 多少秒后发送,可以将固定的日期转化为时间

NSTimeInterval time = [[NSDate dateWithTimeIntervalSinceNow:10] timeIntervalSinceNow];

// NSTimeInterval time = 10;

// repeats,是否重复,如果重复的话时间必须大于60s,要不会报错

UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:time repeats:NO];

 

/*

//如果想重复可以使用这个,按日期

// 周一早上 8:00 上班

NSDateComponents *components = [[NSDateComponents alloc] init];

// 注意,weekday默认是从周日开始

components.weekday = 2;

components.hour = 8;

UNCalendarNotificationTrigger *calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];

*/

// 添加通知的标识符,可以用于移除,更新等操作

NSString *identifier = @"noticeId";

UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];

 

[center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {

NSLog(@"成功添加推送");

}];

}else {

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

// 发出推送的日期

notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];

// 推送的内容

notif.alertBody = @"你已经10秒没出现了";

// 可以添加特定信息

notif.userInfo = @{@"noticeId":@"00001"};

// 角标

notif.applicationIconBadgeNumber = 1;

// 提示音

notif.soundName = UILocalNotificationDefaultSoundName;

// 每周循环提醒

notif.repeatInterval = NSCalendarUnitWeekOfYear;

 

[[UIApplication sharedApplication] scheduleLocalNotification:notif];

}

}

 

 

 

 

NSString *stringInfo=[NSString stringWithFormat:@"APNS error: %@", err];

[self addLocalNotice:stringInfo];