您的位置:首页 > 其它

local/romote_notification

2015-06-29 09:10 218 查看

Local/Romote_Notification

Apple 关于本地通知和远程通知的官方文档

简介

远程通知(remote notification)和本地通知(local notification)是两种用户通知类型,远程通知又称为推送通知(push notification)。这两种通知都可以实现当不在前台运行的APP通知使用该APP的用户新信息,这消息可以是消息,临近的日历,或来自远端服务器的新数据等。当操作系统呈现这些通知时,可以显示一个alert提醒或是app icon提醒。在提醒的时候也可以有声音的提示。

当用户点击通知时可以启动APP查看通知详情,也可选择不打开APP忽略通知。

注意

不管是远程通知合适本地通知都与广播通知(NSNotificationCenter)和KVO(key value observe)无关。

本地通知

Xcode 6本地通知接收不到

出现提示信息:

Attempting to schedule a local notification

with an alert but haven’t received permission from the user to display alerts

with a sound but haven’t received permission from the user to play sounds

原因在于在iOS8系统上需要注册本地通知,这样才能正常使用

iOS8注册本地通知

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

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert categories:nil]];
}

return YES;
}

- (IBAction)registerLocationNotification:(id)sender {

UILocalNotification *locationNotification = [[UILocalNotification alloc]init];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:5];
locationNotification.fireDate = date;
locationNotification.timeZone = [NSTimeZone defaultTimeZone];
locationNotification.repeatInterval = NSCalendarUnitSecond;
locationNotification.alertBody = @"通知来了。。。";
locationNotification.alertAction = @"通知";
locationNotification.alertLaunchImage = @"3.png";
locationNotification.userInfo = @{@"user":@"lfx"};
locationNotification.soundName = @"sound.caf";
locationNotification.applicationIconBadgeNumber = 10;
[[UIApplication sharedApplication]scheduleLocalNotification:locationNotification];
}


回调方法

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
//取消通知的注册
[application cancelLocalNotification:notification];
}


远程通知(Push通知)

APNS推送机制(Apple Push Notification Service)

image1


Provider:程序的后台服务器。

上图分为三阶段:

第一阶段:应用程序的服务器端把要发送的消息、目的iPhone的标识打包,发给APNS。

第二阶段:APNS在自身的已注册Push服务的iPhone列表中,并把消息发送到iPhone。

第三阶段:iPhone把发来的消息传递给应用程序,并按照规定弹出Push通知。

APNS推送通知的详细流程

image2


1.APP注册APNS消息推送,iOS向APNS Server 请求获取device token

2.APNS向APP发送device token ,APP接受device token

3.APP向程序的服务器端发送device token

4.程序服务器端发送Push通知到APNS

5.APNS向APP发送Push通知

远程推送操作过程

1.APP启动过程中,注册远程通知

[[UIApplication sharedApplication]     registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert| UIRemoteNotificationTypeSound];


1.若注册成功,回调方法

-(void)application:(UIApplication *)application     didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(@"%@", deviceToken);
}


1.App获取device token 后,将device token 发送到自己的服务端。

2.APNS服务器得到JSON串后,向App发送通知消息,App调用回调方法

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(@"%@", userInfo);//从userInfo获得推送信息内容
}


客户端的开发

cer文件 provisioning文件的生成

下载生成的cer文件和provi文件,双击导入Xcode,在build setting中code signing一栏中选择这两个文件的名称,这样就可以将支持push的app部署到真机中。然后消息推送:

客户端对推送消息的处理分为两种情况

1.在App没有运行的情况下,系统受到推送消息,用户点击推送消息,启动App。此时,不会执行前面提到的didReceiveRemoteNotification方法,而是App的applicationDidFinishLaunching方法中进行推送处理,通过以下代码可以获取推送消息中数据:

Dictionary *userInfo =[launchOptions objectForKey:UIApplicationLaunchOp

tionsRemoteNotificationKey];

2.当App处于前台,系统受到推送消息,此时系统不会推送消息提示,会直接触发application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo方法,推送数据在userInfo中。

当App处于后台时,如果系统收到推送消息时,当用户点击推送消息时,会执行application: (UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo方法,此时AppDelegate中方法的执行顺序为:applicationWillEnterForeground,applicationWillEnterForeground,applicationDidBecomeActive
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  本和远程通知