ios本地推送demo

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //應用圖標數字
    application.applicationIconBadgeNumber=6;
    
    //申請用戶的許可
    float version=[[[UIDevice currentDevice]systemVersion]floatValue];
    if (version>8.0) {
        UIUserNotificationSettings *setting=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];
        [[UIApplication sharedApplication]registerUserNotificationSettings:setting];
    }
    UILocalNotification *localNotification=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotification!=nil) {
        [self processLocationNotification:localNotification];
    }
    
    return YES;
}
- (void)processLocationNotification:(UILocalNotification *)localNotification{
    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"lanchApp" message:@"loc" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}

//註冊設置提醒後   調用的代理方法
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
    if (notificationSettings.types!=UIUserNotificationTypeNone) {
        //註冊本地推送  首先是生成UILocalNotification對象
        UILocalNotification *localNotification=[[UILocalNotification alloc]init];
        //提示觸發的時間
        localNotification.fireDate=[NSDate dateWithTimeIntervalSinceNow:5];
        //提示推送的內容
        localNotification.alertBody=@"這是一個本地推送測試";
        localNotification.repeatInterval=3;
        localNotification.repeatInterval=NSCalendarUnitMinute;
        //定製消息到來時候播放的聲音文件  必定要在Bunddle內,並且聲音的持續時間不能超過30s
        localNotification.soundName=@"CAT2.WV";
        //設置系統角標
        localNotification.applicationIconBadgeNumber=6;
        //註冊本地通知到系統中,這樣系統到指定的時間會觸發該通知
        [[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
    }
}
//當程序運行在後臺的時候或者程序沒有啓動,,當註冊的本地通知到達時候,ios會彈框而且播放你設置的聲音
//當應用程序運行在前臺的時候會調用該代理方法  不會播放聲音也不會彈框
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    //判斷應用程序狀態來決定是否彈框
    if (application.applicationState == UIApplicationStateActive) {
        UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"本地推送" message:notification.alertBody delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
        [alterView show];
    }else if (application.applicationState == UIApplicationStateInactive)
    {
        NSLog(@"UIApplicationStateInactive");
    }else{
        //background
        NSLog(@"UIApplicationStateBackground");
    }

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