iOS笔记UI--本地推送

//
//  AppDelegate.m
//  本地推送
//
//  Created by hhg on 15/10/23.
//  Copyright (c) 2015年 hhg. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //本地推送 网络推送  都要先注册推送通知
    if ([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        //本地推送
        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];
        [[UIApplication sharedApplication]registerUserNotificationSettings:setting];
    }
    //网络推送
    //    UIRemoteNotificationType type = UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound;
    //    [[UIApplication sharedApplication]registerForRemoteNotificationTypes:type];

    //    if (launchOptions != nil) {
    //        UILocalNotification *local = launchOptions[UIApplicationLaunchOptionsAnnotationKey];
    //        //UIApplicationLaunchOptionsAnnotationKey 本地推送的消息
    //        //UIApplicationLaunchOptionsRemoteNotificationKey 网络推送的消息
    //        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"程序启动时的本地推送" message:local.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    //        [alertView show];
    //
    //    }

    UILocalNotification *local = [[UILocalNotification alloc]init];

    local.alertBody = @"hi,又有新的活动了";

    //推送数量
    local.applicationIconBadgeNumber = 1;

    //推送时间
    local.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];

    //重复周期
    local.repeatInterval = NSCalendarUnitMinute;

    //推送声音
    local.soundName = UILocalNotificationDefaultSoundName;

    //发送本地推送,推送交给系统发送,即使程序启动,系统也会将此推送发给用户
    [[UIApplication sharedApplication]scheduleLocalNotification:local];


    return YES;
}

//当前用户点击推送所触发的方法
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    if ([[UIApplication sharedApplication]applicationState] == UIApplicationStateInactive) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提醒" message:[notification alertBody] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alertView show];
        application.applicationIconBadgeNumber = 0;
    }

    //取消本地推送
    [[UIApplication sharedApplication]cancelLocalNotification:notification];

    //取消网络推送
    //[[UIApplication sharedApplication]unregisterForRemoteNotifications];
}

- (void)applicationWillResignActive:(UIApplication *)application {
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
}

- (void)applicationWillTerminate:(UIApplication *)application {
}
@end

你可能感兴趣的:(OC--UI功能)