一授艰、本地推送
iOS 推送通知分為本地推送和遠(yuǎn)程推送通知胶哲,遠(yuǎn)程推送通知就類似于我們平時(shí)使用微信時(shí)纲岭,即使鎖屏了拥刻,也能收到好友發(fā)送給我們的消息怜瞒,然后在主屏幕顯示一個(gè)alertview,遠(yuǎn)程推送需要遠(yuǎn)程服務(wù)端的支持般哼,比較復(fù)雜. 本地推送相對(duì)比較簡(jiǎn)單吴汪,不需要服務(wù)端的支持。
本地通知是NSLocalNotification 實(shí)現(xiàn)的蒸眠,通過(guò)實(shí)例化一個(gè)NSLocalNotification類型的通知漾橙,同時(shí)設(shè)置通知的fireDate 屬性,即通知的觸發(fā)時(shí)間楞卡;設(shè)置timeZone屬性霜运,即時(shí)區(qū);設(shè)置alertBody蒋腮,顯示的內(nèi)容淘捡;設(shè)置alertAction;設(shè)置soundName,即推送發(fā)生時(shí)的聲音池摧;設(shè)置applicationIconBadgeNumber焦除,即圖標(biāo)上的數(shù)字;設(shè)置userInfo屬性险绘,該屬性是一個(gè)NSDictionary類型的變量踢京。然后在使用UIApplication 的 實(shí)例方法scheduleLocalNotification:或 presentLocalNotificationNow: 推送通知。
**** 1宦棺、創(chuàng)建本地推送 ****
// 創(chuàng)建一個(gè)本地推送
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
//設(shè)置10秒之后
NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10];
if (notification != nil) {
// 設(shè)置推送時(shí)間
notification.fireDate = pushDate;
//推送時(shí)區(qū)設(shè)置:從網(wǎng)上搜到
//timeZone是UILocalNotification激發(fā)時(shí)間是否根據(jù)時(shí)區(qū)改變而改變,如果設(shè)置為nil的話黔帕,
//那么UILocalNotification將在一段時(shí)候后被激發(fā)代咸,而不是某一個(gè)確切時(shí)間被激發(fā)。
notification.timeZone = [NSTimeZone defaultTimeZone];
// 設(shè)置重復(fù)間隔,若不設(shè)置將只會(huì)推送1次
notification.repeatInterval = kCFCalendarUnitDay;
// 推送聲音,(若不設(shè)置的話系統(tǒng)推送時(shí)會(huì)無(wú)聲音)
notification.soundName = UILocalNotificationDefaultSoundName;
// 推送內(nèi)容,(若不設(shè)置成黄,推送中心中不顯示文字呐芥,有聲音提示前提是設(shè)置有聲音)
notification.alertBody = @"推送內(nèi)容";
//推送時(shí)小圖標(biāo)的設(shè)置,PS:這個(gè)東西不知道還有啥用
notification.alertLaunchImage=[[NSBundle mainBundle]pathForResource:@"3" ofType:@"jpg"];
//顯示在icon上的紅色圈中的數(shù)子
notification.applicationIconBadgeNumber = 1;
//設(shè)置userinfo 方便在之后需要撤銷(xiāo)的時(shí)候使用
NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"];
notification.userInfo = info;
//講推送設(shè)置以及信息加入
UIApplication* app=[UIApplication sharedApplication];
BOOL status=YES;
for (UILocalNotification* notification in app.scheduledLocalNotifications)
{
if ([notification.userInfo objectForKey:@"key"]) {
status=NO;
}
}
if (status) {
//加入推送(只能加入一次)
[app scheduleLocalNotification:notification];
}
}
**** 2奋岁、接收本地推送 ****
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iWeibo" message:notification.alertBody delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];
[alert show];
// 圖標(biāo)上的數(shù)字減1
application.applicationIconBadgeNumber -= 1;
}
**** 3思瘟、解除本地推送 ****
// 獲得 UIApplication
UIApplication *app = [UIApplication sharedApplication];
//獲取本地推送數(shù)組
NSArray *localArray = [app scheduledLocalNotifications];
//聲明本地通知對(duì)象
UILocalNotification *localNotification;
if (localArray) {
for (UILocalNotification *noti in localArray) {
NSDictionary *dict = noti.userInfo;
if (dict) {
NSString *inKey = [dict objectForKey:@"key"];
if ([inKey isEqualToString:@"對(duì)應(yīng)的key值"]) {
if (localNotification){
[localNotification release];
localNotification = nil;
}
localNotification = [noti retain];
break;
}
}
}
//判斷是否找到已經(jīng)存在的相同key的推送
if (!localNotification) {
//不存在初始化
localNotification = [[UILocalNotification alloc] init];
}
if (localNotification) {
//不推送 取消推送
[app cancelLocalNotification:localNotification];
[localNotification release];
return;
}
}
二、遠(yuǎn)程推送
閱讀參考鏈接闻伶。
**** 參考鏈接 ****
本地推送
遠(yuǎn)程推送:
- http://blog.csdn.net/enuola/article/details/8627283
- http://www.cnblogs.com/yh-qfnu/p/3269768.html
- http://www.cocoachina.com/ios/20100401/900.html
- http://blog.csdn.net/dalehui/article/details/16807157
- http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
- http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2
- http://mobiforge.com/design-development/programming-apple-push-notification-services
- http://segmentfault.com/a/1190000000520755
- 極光推送文檔