第一步:创建本地推送
UILocalNotification
*notification = [[[
UILocalNotification
alloc
]
init
]
autorelease
];
NSDate
*pushDate = [
NSDate
dateWithTimeIntervalSinceNow
:
1
0
];
if
(notification !=
nil
) {
notification
.fireDate
= pushDate;
notification
.timeZone
= [
NSTimeZone
defaultTimeZone
];
notification
.repeatInterval
= kCFCalendarUnitDay;
notification
.soundName
=
UILocalNotificationDefaultSoundName
;
notification
.alertBody
=
@"推送内容"
;
notification
.applicationIconBadgeNumber
=
1
;
NSDictionary
*info = [
NSDictionary
dictionaryWithObject
:
@"name"
forKey:
@"key"
];
notification
.userInfo
= info;
UIApplication
*app = [
UIApplication
sharedApplication
];
[app
scheduleLocalNotification
:notification];
}
第二步:接收本地推送
- (
void
)application:(
UIApplication
*)application
didReceiveLocalNotification
:(UILocalNotification*)notification{
UIAlertView
*alert = [[
UIAlertView
alloc
]
initWithTitle
:
@"iWeibo"
message
:notification
.alertBody
delegate
:nil
cancelButtonTitle
:
@"确定"
otherButtonTitles
:nil
];
[alert
show
];
application
.applicationIconBadgeNumber
-=
1
;
}
第三步:解除本地推送
UIApplication
*app = [
UIApplication
sharedApplication
];
NSArray
*localArray = [app
scheduledLocalNotifications
];
UILocalNotification
*localNotification;
if
(localArray) {
for
(
UILocalNotification
*noti
in
localArray) {
NSDictionary
*dict = noti
.userInfo
;
if
(dict) {
NSString
*inKey = [dict
objectForKey
:
@"key"
];
if
([inKey
isEqualToString
:
@"对应的key值"
]) {
if
(localNotification){
[localNotification
release
];
localNotification =
nil
;
}
localNotification = [noti
retain
];
break;
}
}
}
if
(!localNotification) {
localNotification = [[
UILocalNotification
alloc
]
init
];
}
if
(localNotification) {
[app
cancelLocalNotification
:localNotification];
[localNotification
release
];
return
;
}
}