本地推送UILocalNotification(轉)

1、增長一個本地推送

//設置20秒以後 

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:20];

    //chuagjian一個本地推送

    UILocalNotification *noti = [[[UILocalNotification alloc] init] autorelease];

    if (noti) {

        //設置推送時間

        noti.fireDate = date;

        //設置時區

        noti.timeZone = [NSTimeZone defaultTimeZone];

        //設置重複間隔

        noti.repeatInterval = NSWeekCalendarUnit;

        //推送聲音

        noti.soundName = UILocalNotificationDefaultSoundName;

        //內容

        noti.alertBody = @"推送內容";

        //顯示在icon上的紅色圈中的數子

        noti.applicationIconBadgeNumber = 1;

        //設置userinfo 方便在以後須要撤銷的時候使用

        NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];

        noti.userInfo = infoDic;

        //添加推送到uiapplication        

        UIApplication *app = [UIApplication sharedApplication];

        [app scheduleLocalNotification:noti];  

    }

二、程序運行時接收到本地推送消息數組

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"接收到本地提醒 in app"

message:notification.alertBody

   delegate:nil

  cancelButtonTitle:@"肯定"

  otherButtonTitles:nil];

[alert show];

//這裏,你就能夠經過notification的useinfo,幹一些你想作的事情了

application.applicationIconBadgeNumber -= 1;

}

三、取消一個本地推送app

UIApplication *app = [UIApplication sharedApplication];

    //獲取本地推送數組

    NSArray *localArr = [app scheduledLocalNotifications];

    

    //聲明本地通知對象

    UILocalNotification *localNoti;

    

    if (localArr) {

        for (UILocalNotification *noti in localArr) {

            NSDictionary *dict = noti.userInfo;

            if (dict) {

                NSString *inKey = [dict objectForKey:@"key"];

                if ([inKey isEqualToString:key]) {

                    if (localNoti){

                        [localNoti release];

                        localNoti = nil;

                    }

                    localNoti = [noti retain];

                    break;

                }

            }

        }

        

        //判斷是否找到已經存在的相同key的推送

        if (!localNoti) {

            //不存在 初始化

            localNoti = [[UILocalNotification alloc] init];

        }

        

        if (localNoti && !state) {

            //不推送 取消推送

            [app cancelLocalNotification:localNoti];

            [localNoti release];

            return;

        }

}
相關文章
相關標籤/搜索