/* 推送
分类
- 1、远程推送(Remote Notification)
- 2、本地推送 (Local Notification)
作用
可以让APP不在前台,告知用户 APP内部发生了什么事
效果
1、没有效果
2、在屏幕顶部 显示一块横幅(显示具体的内容)
3、在屏幕的中间弹出一个AlertController(也是显示具体内容)
4、在锁屏的时候,显示一块横幅
5、可以更改APP上面显示的提醒数字
- 播放音效
- 推送通知的使用细节
注意:发送推送通知的时候,如果APP在前台运行,那么推送的通知不会被呈现出来
在发送通知之后,无论APP是打开,还是关闭,推送都能如期发出,但是用户 不一定能如期去接收
*/
- 本地推送(本地通知)
在iOS8之后,以前的本地推送写法可能会出错,接收不到推送的信息,
如果出现以下信息:
1 Attempting to schedule a local notification 2 with an alert but haven't received permission from the user to display alerts 3 with a sound but haven't received permission from the user to play sounds
说明在IOS8下没有注册,所以需要额外添加对IOS8的注册方法,API中有下面这个方法:
// Registering UIUserNotificationSettings more than once results in previous settings being overwritten. - (void)registerUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0);
这个方法是8.0之后才能使用的,所以需要判断一下系统的版本。
第一步:注册本地通知:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/*
UIUserNotificationTypeNone = 0, 无类型(不给用户发通知)
UIUserNotificationTypeBadge = 1 << 0, 是否可以改变应用图标右上角的提示数字
UIUserNotificationTypeSound = 1 << 1, 该通知是否会有声音
UIUserNotificationTypeAlert = 1 << 2, 是否有弹出提示
*/
if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
// 跳转
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 300, 300, 300);
label.backgroundColor = [UIColor redColor];
label.text = [NSString stringWithFormat:@"%@", launchOptions];
label.font = [UIFont systemFontOfSize:14];
label.numberOfLines = 0;
[self.window.rootViewController.view addSubview:label];
}
return YES;
}
/** * 点击通知打开应用的时候会执行该方法 * 应用在前台的时候,收到通知也会执行该方法 * * @param notification 通知 */ - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { UILabel *label = [[UILabel alloc] init]; label.frame = CGRectMake(0, 0, 300, 300); label.backgroundColor = [UIColor colorWithRed:0.871 green:1.000 blue:0.797 alpha:1.000]; label.text = [NSString stringWithFormat:@"%@", notification]; label.font = [UIFont systemFontOfSize:14]; label.numberOfLines = 0; [self.window.rootViewController.view addSubview:label]; // if (application.applicationState == UIApplicationStateBackground) { // // } }
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return YES;
}
这里我们添加两个方法,来进行推送消息的操作;
/** * 点击按钮后添加本地通知 */ - (IBAction)addLocalNote; /** * 移除通知(不常用) */ - (IBAction)removeLocalNote;
实现方法(点击按钮之后添加本地通知):
- (IBAction)addLocalNote { /* @property(nonatomic,copy) NSDate *fireDate; @property(nonatomic,copy) NSTimeZone *timeZone; @property(nonatomic) NSCalendarUnit repeatInterval; @property(nonatomic,copy) NSCalendar *repeatCalendar; @property(nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0); @property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0); @property(nonatomic,copy) NSString *alertBody; @property(nonatomic) BOOL hasAction; @property(nonatomic,copy) NSString *alertAction; @property(nonatomic,copy) NSString *alertLaunchImage; @property(nonatomic,copy) NSString *soundName; UILocalNotificationDefaultSoundName @property(nonatomic) NSInteger applicationIconBadgeNumber; @property(nonatomic,copy) NSDictionary *userInfo; */ // 1.创建一个本地通知 UILocalNotification *localNote = [[UILocalNotification alloc] init]; // 1.1.设置通知发出的时间 localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; // 1.2.设置通知发出的内容 localNote.alertBody = @"我是一条推送消息"; // 1.3.是否弹出提示框 localNote.hasAction = YES; // 1.4.设置提示框 localNote.alertAction = @"赶紧查看"; // 1.5.设置启动的图片 localNote.alertLaunchImage = @"backk.jpg"; // 1.6.设置启动的音效 localNote.soundName = UILocalNotificationDefaultSoundName; // 1.7.设置应用图标提醒的数字 localNote.applicationIconBadgeNumber = 666; // 1.8.如果想将通知的信息传递过去,必须使用userInfo属性 localNote.userInfo = @{@"msg" : @"吃饭了吗", @"date" : localNote.fireDate}; // 2.调度通知 [[UIApplication sharedApplication] scheduleLocalNotification:localNote]; [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; } - (IBAction)removeLocalNote { [[UIApplication sharedApplication] cancelAllLocalNotifications]; // [UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *) }
注意;在此也需要在真机上操作,因为模拟器,不显示。。。。。。