ios 交流群:264706196
本地推送
本地通知是由本地應(yīng)用觸發(fā)的审洞,它是基于時(shí)間行為的一種通知形式,例如鬧鐘定時(shí)、待辦事項(xiàng)提醒锅睛,又或者一個(gè)應(yīng)用在一段時(shí)候后不使用通常會(huì)提示用戶使用此應(yīng)用等都是本地通知。創(chuàng)建一個(gè)本地通知通常分為以下幾個(gè)步驟:
- 創(chuàng)建UILocalNotification历谍。
- 設(shè)置處理通知的時(shí)間fireDate现拒。
- 配置通知的內(nèi)容:通知主體、通知聲音扮饶、圖標(biāo)數(shù)字等具练。
- 配置通知傳遞的自定義數(shù)據(jù)參數(shù)userInfo(這一步可選)。
- 調(diào)用通知甜无,可以使用scheduleLocalNotification:按計(jì)劃調(diào)度一個(gè)通知扛点,也可以使用presentLocalNotificationNow立即調(diào)用通知。
//
// AppDelegate.m
// LocalNotification
//
// Created by Kenshin Cui on 14/03/28.
// Copyright (c) 2014年 Kenshin Cui. All rights reserved.
//
#import "AppDelegate.h"
#import "KCMainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
#pragma mark - 應(yīng)用代理方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
_window.backgroundColor =[UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1];
//設(shè)置全局導(dǎo)航條風(fēng)格和顏色
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:23/255.0 green:180/255.0 blue:237/255.0 alpha:1]];
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
KCMainViewController *mainController=[[KCMainViewController alloc]init];
_window.rootViewController=mainController;
[_window makeKeyAndVisible];
//如果已經(jīng)獲得發(fā)送通知的授權(quán)則創(chuàng)建本地通知岂丘,否則請(qǐng)求授權(quán)(注意:如果不請(qǐng)求授權(quán)在設(shè)置中是沒有對(duì)應(yīng)的通知設(shè)置項(xiàng)的陵究,也就是說如果從來沒有發(fā)送過請(qǐng)求,即使通過設(shè)置也打不開消息允許設(shè)置)
if ([[UIApplication sharedApplication]currentUserNotificationSettings].types!=UIUserNotificationTypeNone) {
[self addLocalNotification];
}else{
[[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
return YES;
}
#pragma mark 調(diào)用過用戶注冊(cè)通知方法之后執(zhí)行(也就是調(diào)用完registerUserNotificationSettings:方法之后執(zhí)行)
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
if (notificationSettings.types!=UIUserNotificationTypeNone) {
[self addLocalNotification];
}
}
#pragma mark 進(jìn)入前臺(tái)后設(shè)置消息信息
-(void)applicationWillEnterForeground:(UIApplication *)application{
[[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];//進(jìn)入前臺(tái)取消應(yīng)用消息圖標(biāo)
}
#pragma mark - 私有方法
#pragma mark 添加本地通知
-(void)addLocalNotification{
//定義本地通知對(duì)象
UILocalNotification *notification=[[UILocalNotification alloc]init];
//設(shè)置調(diào)用時(shí)間
notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:10.0];//通知觸發(fā)的時(shí)間,10s以后
notification.repeatInterval=2;//通知重復(fù)次數(shù)
//notification.repeatCalendar=[NSCalendar currentCalendar];//當(dāng)前日歷侣肄,使用前最好設(shè)置時(shí)區(qū)等信息以便能夠自動(dòng)同步時(shí)間
//設(shè)置通知屬性
notification.alertBody=@"最近添加了諸多有趣的特性颗搂,是否立即體驗(yàn)?"; //通知主體
notification.applicationIconBadgeNumber=1;//應(yīng)用程序圖標(biāo)右上角顯示的消息數(shù)
notification.alertAction=@"打開應(yīng)用"; //待機(jī)界面的滑動(dòng)動(dòng)作提示
notification.alertLaunchImage=@"Default";//通過點(diǎn)擊通知打開應(yīng)用時(shí)的啟動(dòng)圖片,這里使用程序啟動(dòng)圖片
//notification.soundName=UILocalNotificationDefaultSoundName;//收到通知時(shí)播放的聲音松蒜,默認(rèn)消息聲音
notification.soundName=@"msg.caf";//通知聲音(需要真機(jī)才能聽到聲音)
//設(shè)置用戶信息
notification.userInfo=@{@"id":@1,@"user":@"Kenshin Cui"};//綁定到通知上的其他附加信息
//調(diào)用通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
#pragma mark 移除本地通知,在不需要此通知時(shí)記得移除
-(void)removeNotification{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
@end
參考資料:
http://www.cnblogs.com/kenshincui/p/4168532.html#localNotification