iOS推送的那些事
直接切入主题,讲讲如何模拟推送以及处理推送消息。在进入主题之前,我先说几个关键流程: Provider:就是为指定iOS设备应用程序提供Push的服务器。如果iOS设备的应用程序是客户端的话,那么Provider可以理解为服务端(推送消息的发起者)
上述第一个步骤注册的 API 是 iOS 8 新增的,因此在 iOS 7,前两个步骤需更改为 iOS 7 中的 API。 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { NSLog(@"Requesting permission for push notifications..."); // iOS 8 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [UIApplication.sharedApplication registerUserNotificationSettings:settings]; } else { NSLog(@"Registering device for push notifications..."); // iOS 7 and earlier [UIApplication.sharedApplication registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound]; } return YES; } - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)settings { NSLog(@"Registering device for push notifications..."); // iOS 8 [application registerForRemoteNotifications]; } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token { NSLog(@"Registration successful,bundle identifier: %@,mode: %@,device token: %@",[NSBundle.mainBundle bundleIdentifier],[self modeString],token); } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"Failed to register: %@",error); } - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)notification completionHandler:(void(^)())completionHandler { NSLog(@"Received push notification: %@,identifier: %@",notification,identifier); // iOS 8 completionHandler(); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification { NSLog(@"Received push notification: %@",notification); // iOS 7 and earlier } - (NSString *)modeString { #if DEBUG return @"Development (sandbox)"; #else return @"Production"; #endif } 2、处理推送消息 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //... NSDictionary *payload = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (payload) { //... } //... } 2)、程序在前台运行,接收到消息不会有消息提示(提示框或横幅)。当程序运行在后台,接收到消息会有消息提示,点击消息后进入程序,AppDelegate的didReceiveRemoteNotification函数会被调用,消息做为此函数的参数传入 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)payload { NSLog(@"remote notification: %@",[payload description]); NSString* alertStr = nil; NSDictionary *apsInfo = [payload objectForKey:@"aps"]; NSObject *alert = [apsInfo objectForKey:@"alert"]; if ([alert isKindOfClass:[NSString class]]) { alertStr = (NSString*)alert; } else if ([alert isKindOfClass:[NSDictionary class]]) { NSDictionary* alertDict = (NSDictionary*)alert; alertStr = [alertDict objectForKey:@"body"]; } application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue]; if ([application applicationState] == UIApplicationStateActive && alertStr != nil) { UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Pushed Message" message:alertStr delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } } 3、义通知提示音
你可以将它们打包为aiff、wav或caf文件。自定义的声音文件时间必须小于 30秒,如果超过了这个时间,将被系统声音代替。 { "aps" : {"alert" : "This is the alert text","badge" : 1,"sound" :"default" }, "server" : {"serverId" : 1,"name" : "Server name"} } 这样收到的 Payload 里面会多一个 server 的字段。 <?php // devicetoken $deviceToken = '你的deviceToken'; // 私钥密码,生成pem的时候输入的 $passphrase = '123456'; // 定制推送内容,有一点的格式要求,详情Apple文档 $message = array( 'body'=>'你收到一个新订单' ); $body['aps'] = array( 'alert' => $message,'sound' => 'default','badge' => 100,); $body['type']=3; $body['msg_type']=4; $body['title']='新订单提醒'; $body['msg']='你收到一个新消息'; $ctx = stream_context_create(); stream_context_set_option($ctx,'ssl','local_cert','push.pem');//记得把生成的push.pem放在和这个php文件同一个目录 stream_context_set_option($ctx,'passphrase',$passphrase); $fp = stream_socket_client( //这里需要特别注意,一个是开发推送的沙箱环境,一个是发布推送的正式环境,deviceToken是不通用的 'ssl://gateway.sandbox.push.apple.com:2195',$err,//'ssl://gateway.push.apple.com:2195',$errstr,60,STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT,$ctx); if (!$fp) exit("Failed to connect: $err $errstr" . PHP_EOL); echo 'Connected to APNS' . PHP_EOL; $payload = json_encode($body); $msg = chr(0) . pack('n',32) . pack('H*',$deviceToken) . pack('n',strlen($payload)) . $payload; $result = fwrite($fp,$msg,strlen($msg)); if (!$result) echo 'Message not delivered' . PHP_EOL; else echo 'Message successfully delivered' . PHP_EOL; fclose($fp); ?> 将上面的代码复制,保存成push.php superdanny@SuperDannydeMacBook-Pro$ php push.php 结果为 Connected to APNS Message successfully delivered 本文已被整理到了《iOS推送教程》,欢迎大家学习阅读。 以上就是关于IOS推送的那些事,希望对大家的学习有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |