添加本地通知需要進(jìn)入項目的project->Capabilities把Push Notifications打開允耿,并添加一下方法,注意ios8后提澎,需要添加注冊念链,才能得到授權(quán)
// 設(shè)置本地通知
-(void)registerLocalNotificationWithTime:(NSTimeInterval)startTime notifiTitle:(NSString *)title notifiText:(NSString *)text key:(NSString *)key
{
//避免重復(fù)添加
[self cancelLocalNotificationWithKey:key];
if (startTime <= 0)//返回的時間點小于當(dāng)前時間掂墓,不彈
{
return;
}
UILocalNotification *notification = [[UILocalNotification alloc] init];
// 設(shè)置觸發(fā)通知的時間
NSDate *fireDate = [NSDate dateWithTimeIntervalSince1970:startTime];
fireDate = [NSDate dateWithTimeIntervalSinceNow:startTime];
notification.fireDate = fireDate;
// 時區(qū)
notification.timeZone = [NSTimeZone defaultTimeZone];
// 通知內(nèi)容
notification.alertBody = text;
notification.applicationIconBadgeNumber ++;
// 通知被觸發(fā)時播放的聲音
notification.soundName = UILocalNotificationDefaultSoundName;
// 通知參數(shù)
NSDictionary *notificationDic = @{@"key":key ?:@"", @"title":title?:@"",@"content":text?:@""};
NSDictionary *userDict = [NSDictionary dictionaryWithObject:notificationDic forKey:@"userInfo"];
notification.userInfo = userDict;
// ios8后君编,需要添加這個注冊川慌,才能得到授權(quán)
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 通知重復(fù)提示的單位祠乃,可以是天亮瓷、周降瞳、月
// notification.repeatInterval = NSCalendarUnitDay;
} else {
// 通知重復(fù)提示的單位,可以是天除师、周扔枫、月
// notification.repeatInterval = NSDayCalendarUnit;
}
// 執(zhí)行通知注冊
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
在appDelegate中:
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSDictionary *notDic = [localNotif.userInfo objectForKey:@"userInfo"];
// ASSERT_Class(notDic, NSDictionary);
NSString *notKey = [notDic safe_stringForKey:@"key"];//直播 master_id
self.liveKey = notKey;
[__LiveManager jumptoNextPageWithMasterId:notKey vc:__lolita.topVC completeBlock:nil];
// 更新顯示的徽章個數(shù)
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
badge--;
badge = badge >= 0 ? badge : 0;
[UIApplication sharedApplication].applicationIconBadgeNumber = badge;
}