一、推送通知
UILocalNotification *ln = [[UILocalNotification alloc] init];
@property(nonatomic,copy) NSDate *fireDate;
@property(nonatomic,copy) NSString *alertBody;
@property(nonatomic,copy) NSString *alertAction;
@property(nonatomic,copy) NSString *soundName;
@property(nonatomic) NSInteger applicationIconBadgeNumber;
[[UIApplication sharedApplication] scheduleLocalNotification:ln];
@property(nonatomic,copy) NSArray *scheduledLocalNotifications;
(已经发出且过期的推送通知就算调度结束,会自动从这个数组中移除)
- (void)cancelLocalNotification:(UILocalNotification *)notification;
- (void)cancelAllLocalNotifications;
- (void)presentLocalNotificationNow:(UILocalNotification *)notification;
@property(nonatomic) NSCalendarUnit repeatInterval;
@property(nonatomic,copy) NSString *alertLaunchImage;
@property(nonatomic,copy) NSDictionary *userInfo;
@property(nonatomic,copy) NSTimeZone *timeZone;
(一般设置为[NSTimeZone defaultTimeZone] ,跟随手机的时区)
4.点击本地推送通知
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
三、远程推送通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 注册远程通知
UIRemoteNotificationType type = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:type];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"%@", deviceToken);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
一.开发iOS程序的推送功能, iOS端需要做的事
1.请求苹果获得deviceToken
2.得到苹果返回的deviceToken
3.发送deviceToken给公司的服务器
4.监听用户对通知的点击
二.调试iOS的远程推送功能, 必备条件:
1.真机
2.调试推送需要的证书文件
1> aps_development.cer : 某台电脑就能调试某个app的推送服务
2> ios_development.cer : 让电脑具备真机调试的能力(调试设备)
3> iphone5_qq.mobileprovision : 某台电脑就能利用某台设备调试某个程序
三.发布具有推送服务的app
1> aps_production.cer : 如果发布的程序中包含了推送服务,就必须安装这个证书
2> ios_distribution.cer : 让电脑具备发布程序的能力
3> qq.mobileprovision : 某台电脑就能发布某个程序
远程推送
//
// AppDelegate.m
// 01-获取DeviceToken
//
// Created by apple on 14/11/10.
// Copyright (c) 2014年 heima. All rights reserved.
// #import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. if ([UIDevice currentDevice].systemVersion.doubleValue <= 8.0) {
// 不是iOS8
UIRemoteNotificationType type = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert;
// 当用户第一次启动程序时就获取deviceToke
// 该方法在iOS8以及过期了
// 只要调用该方法, 系统就会自动发送UDID和当前程序的Bunle ID到苹果的APNs服务器
[application registerForRemoteNotificationTypes:type];
}else
{
// iOS8
UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
// 注册通知类型
[application registerUserNotificationSettings:settings]; // 申请试用通知
[application registerForRemoteNotifications];
} // 1.取出数据
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; if (userInfo) {
static int count = ;
count++;
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(, , , );
label.numberOfLines = ;
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:];
label.backgroundColor = [UIColor orangeColor];
label.text = [NSString stringWithFormat:@" %@ \n %d", userInfo, count];
[self.window.rootViewController.view addSubview:label];
} return YES;
} /**
* 获取到用户对应当前应用程序的deviceToken时就会调用
*/
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"%@", deviceToken);
// <47e58207 31340f18 ed83ba54 f999641a 3d68bc7b f3e2db29 953188ec 7d0cecfb>
// <286c3bde 0bd3b122 68be655f 25ed2702 38e31cec 9d54da9f 1c62325a 93be801e>
} /*
ios7以前苹果支持多任务, iOS7以前的多任务是假的多任务
而iOS7开始苹果才真正的推出了多任务
*/
// 接收到远程服务器推送过来的内容就会调用
// 注意: 只有应用程序是打开状态(前台/后台), 才会调用该方法
/// 如果应用程序是关闭状态会调用didFinishLaunchingWithOptions
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
/*
如果应用程序在后台 , 只有用户点击了通知之后才会调用
如果应用程序在前台, 会直接调用该方法
即便应用程序关闭也可以接收到远程通知
*/
NSLog(@"%@", userInfo); // static int count = 0;
// count++;
// UILabel *label = [[UILabel alloc] init];
// label.frame = CGRectMake(0, 250, 200, 200);
// label.numberOfLines = 0;
// label.textColor = [UIColor whiteColor];
// label.text = [NSString stringWithFormat:@"%@ \n %d", userInfo, count];
// label.font = [UIFont systemFontOfSize:11];
// label.backgroundColor = [UIColor grayColor];
// [self.window.rootViewController.view addSubview:label];
} //接收到远程服务器推送过来的内容就会调用
// ios7以后用这个处理后台任务接收到得远程通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
/*
UIBackgroundFetchResultNewData, 成功接收到数据
UIBackgroundFetchResultNoData, 没有;接收到数据
UIBackgroundFetchResultFailed 接收失败 */
// NSLog(@"%s",__func__);
// NSLog(@"%@", userInfo); NSNumber *contentid = userInfo[@"content-id"];
if (contentid) {
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(, , , );
label.numberOfLines = ;
label.textColor = [UIColor whiteColor];
label.text = [NSString stringWithFormat:@"%@", contentid];
label.font = [UIFont systemFontOfSize:];
label.backgroundColor = [UIColor grayColor];
[self.window.rootViewController.view addSubview:label];
//注意: 在此方法中一定要调用这个调用block, 告诉系统是否处理成功.
// 以便于系统在后台更新UI等操作
completionHandler(UIBackgroundFetchResultNewData);
}else
{
completionHandler(UIBackgroundFetchResultFailed);
} }
@end
本地推送
//
// MJViewController.m
// 06-本地通知
//
// Created by apple on 14-6-4.
// Copyright (c) 2014年 itcast. All rights reserved.
// #import "MJViewController.h" @interface MJViewController ()
- (IBAction)addLocalNote;
- (IBAction)cancelLocalNote; @end @implementation MJViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (IBAction)addLocalNote {
// 1.创建通知
UILocalNotification *localNote = [[UILocalNotification alloc] init]; // 2.设置属性
localNote.alertAction = @"开始玩游戏"; // 操作标题
localNote.alertBody = @"都好几天了, 你赶紧用一下我吧!!!"; // 正文
localNote.applicationIconBadgeNumber = ;
// localNote.repeatInterval = NSCalendarUnitMinute;
localNote.alertLaunchImage = @"Default"; // 点击通知, 打开程序时候现实的启动图片
localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:];
// 3.注册通知(添加)
UIApplication *app = [UIApplication sharedApplication];
[app cancelAllLocalNotifications];
[app scheduleLocalNotification:localNote];
} - (IBAction)cancelLocalNote {
UIApplication *app = [UIApplication sharedApplication];
[app cancelAllLocalNotifications];
}
@end
//
// MJAppDelegate.m
// 06-本地通知
//
// Created by apple on 14-6-4.
// Copyright (c) 2014年 itcast. All rights reserved.
// #import "MJAppDelegate.h" @interface MJAppDelegate()
//@property (nonatomic, weak) UILabel *label;
@end @implementation MJAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(, , , );
label.backgroundColor = [UIColor redColor];
label.text = [NSString stringWithFormat:@"%@", launchOptions];
[self.window.rootViewController.view addSubview:label];
// self.label = label; // Override point for customization after application launch. NSLog(@"didFinishLaunchingWithOptions---");
return YES;
} /**
说明用户点击通知, 进入了程序(程序还在运行中, 程序并没有被关掉)
*/
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(, , , );
label.backgroundColor = [UIColor blueColor];
label.text = @"didReceiveLocalNotification";
[self.window.rootViewController.view addSubview:label]; NSLog(@"didReceiveLocalNotification");
} - (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
} - (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
} - (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
} - (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
} - (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
} @end