本地推送(主要用于耗時(shí)操作,當(dāng)操作完成之后可以通知給用戶)
步驟如下:
1.在app delegate? didFinishLaunchingWithOptions方法中進(jìn)行推送設(shè)置
推送設(shè)置(iOS8.0以后設(shè)置方法發(fā)生改變)
//版本適配問題
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0)//當(dāng)前使用設(shè)備
{
//采取8.0以上的設(shè)置
/**
*? @param UIUserNotificationType 推動通知的類型(聲音/文字/標(biāo)示(徽章))
*? @param categories 額外設(shè)置(設(shè)置額外按鈕)
*/
UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];
//讓當(dāng)前程序接收/注冊推送通知設(shè)置
[application registerUserNotificationSettings:settings];
//詢問用戶是否允許
[application registerForRemoteNotifications];
}
2.開始推送
UILocalNotification * noti = [[UILocalNotification alloc]init];
if (noti) {
//設(shè)置推送通知
//通知推送事件
//開啟事件為當(dāng)前時(shí)間后10秒
noti.fireDate = [[NSDate date] dateByAddingTimeInterval:10];
//重復(fù)次數(shù)
//不重復(fù)
noti.repeatInterval = 0;
//設(shè)置推送時(shí)區(qū)
noti.timeZone = [NSTimeZone defaultTimeZone];
//增加圖標(biāo)標(biāo)示
noti.applicationIconBadgeNumber = 1;
//設(shè)置推送聲音 音頻聲音格式要為蘋果原生支持的
noti.soundName = UILocalNotificationDefaultSoundName;
//推送內(nèi)容
noti.alertBody = @"ajdfjkjljhb";
//設(shè)置推送的攜帶詳情,例如為某影片播放網(wǎng)址
noti.userInfo = @{@"URL":@""};
//執(zhí)行推送
[[UIApplication sharedApplication] scheduleLocalNotification:noti];
}
注:通過推送進(jìn)入App時(shí)會調(diào)用的方法
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"通過推送進(jìn)入APP");
NSDictionary * us = notification.userInfo;
NSLog(@"URL為:%@",us[@"URL"]);
//執(zhí)行跳轉(zhuǎn)
[self.window.rootViewController presentViewController:[[PlayViewController alloc]init] animated:YES completion:nil];
}