推送
1、远程推送(Remote Notification)
2、本地推送(Local Notification)
作用
可以让App不在前台,告知用户App内部发生了什么事
推送通知的呈现效果总结
1.用户接收的推送通知,都会展示在“通知中心”从屏幕顶部往下滑,就能调出“通知中心”,显示横幅还是UIAlertView,取决于用户的设置
2.总结一下,推送通知有5种不同的呈现效果
0)没有效果
1)在屏幕顶部显示一块横幅(显示具体内容)
2)在屏幕中间弹出一个UIAlertView(显示具体内容)
3)在锁屏界面显示一块横幅(锁屏状态下,显示具体内容)
4)更改app图标的数字(说明新内容的数量)
-播放音效
-推送通知的使用细节
注意:发送推送通知的时候,如果APP在前台运行,那么推送的通知不会被呈现出来
在发送通知之后,无论APP是打开还是关闭,通知都能如期发出,但是用户不一定就会如期去接收
下面就是具体的示例:
在AppDelegate.h里面
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//判断系统是否大于8.0
if([UIDevicecurrentDevice].systemVersion.doubleValue>=8.0) {
//设置本地推送的样式
UIUserNotificationSettings*settings = [UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlertcategories:nil];
[applicationregisterUserNotificationSettings:settings];
}
//用作点击推送时候的跳转
if(launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
//跳转
UILabel*label = [[UILabelalloc]init];
label.frame=CGRectMake(0,300,300,300);
label.backgroundColor= [UIColorredColor];
label.text= [NSStringstringWithFormat:@"%@", launchOptions];
label.font= [UIFontsystemFontOfSize:14];
label.numberOfLines=0;
[self.window.rootViewController.viewaddSubview:label];
}
returnYES;
}
本地推送的四种样式:
UIUserNotificationTypeNone= 0,无类型(不给用户发通知)
UIUserNotificationTypeBadge= 1 << 0,是否可以改变应用图标右上角的提示数字
UIUserNotificationTypeSound= 1 << 1,该通知是否会有声音
UIUserNotificationTypeAlert= 1 << 2,是否有弹出提示
点击通知打开应用的时候会执行该方法。应用在前台的时候,收到通知也会执行该方法
- (void)application:(UIApplication*)application didReceiveLocalNotification:
(UILocalNotification*)notification
{
UILabel*label = [[UILabelalloc]init];
label.frame=CGRectMake(0,0,300,300);
label.backgroundColor= [UIColorcolorWithRed:0.871green:1.000blue:0.797alpha:1.000];
label.text= [NSStringstringWithFormat:@"%@", notification];
label.font= [UIFontsystemFontOfSize:14];
label.numberOfLines=0;
[self.window.rootViewController.viewaddSubview:label];
//if (application.applicationState == UIApplicationStateBackground) {
//
//}
}
在viewController.h 里面 首先创建两个button,一个添加通知,一个移除通知,实现button按钮的响应方法。
添加通知的响应事件:
//创建一个本地通知
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
// 2、设置具体的时间
//2.1设置通知发送的时间
localNotification.fireDate= [NSDate dateWithTimeIntervalSinceNow:3];
//2.2设置通知发出的内容
localNotification.alertBody=@"我是一条推送消息";
//2.3设置是否显示提示框
localNotification.hasAction=YES;
//2.4设置提示提示框
localNotification.alertAction=@"赶紧去看看";
//2.5设置app的提醒数字
localNotification.applicationIconBadgeNumber=2;
//2.6设置应用的提示声音
localNotification.soundName=UILocalNotificationDefaultSoundName;
//3、调度通知
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
[[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];
移除通知的响应事件:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
网友评论