这里在程序进入后台和从后台重新启动为例
void AppDelegate::createMyPushNotification()//创建一个推送
{}
void AppDelegate::deleteMyPushNotification()//删除一个推送
{
UIApplication *app = [UIApplication sharedApplication];
//获取本地推送数组
NSArray *localArr = [app scheduledLocalNotifications];
//声明本地通知对象
UILocalNotification *localNoti = NULL;
if (localArr)
{
for (UILocalNotification *noti in localArr)
{
NSDictionary *dict = noti.userInfo;
if (dict)
{
NSString *inKey = [dict objectForKey:@"pushName"];
if ([inKey isEqualToString:@"MyPushNotification"])
{
if (localNoti)
{
[localNoti release];
localNoti = nil;
}
localNoti = [noti retain];
break;
}
}
}
//判断是否找到已经存在的相同key的推送
// if (!localNoti)
// {
// //不存在 初始化
// localNoti = [[UILocalNotification alloc] init];
// }
if (localNoti)
{
//不推送 取消推送
[app cancelLocalNotification:localNoti];
[localNoti release];
return;
}
}
}
//这里在程序启动时需要手动设置一下icon右上角红色圆圈的数字 不然会一直显示
application.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
我是在appcontroller中实现的
这里还有几个代理可以实现,在代理方法中可以实现自己想要做的操作
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
application.applicationIconBadgeNumber = 0;
}
//程序启动时添加以下代码清除推送...
application.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] cancelAllLocalNotifications];