
Provider:就是为指定iOS设备应用程序提供Push的服务器。如果iOS设备的应用程序是客户端的话,那么Provider可以理解为服务端(推送消息的发起者)
APNs:Apple Push Notification Service(苹果消息推送服务器)
Devices:iOS设备,用来接收APNs下发下来的消息
Client App:iOS设备上的应用程序,用来接收APNs下发的消息到指定的一个客户端app(消息的最终响应者)
1、取Device token
App 必须要向 APNs 请求注册以实现推送功能,在请求成功后,APNs 会返回一个设备的标识符即 DeviceToken 给 App,服务器在推送通知的时候需要指定推送通知目的设备的 DeviceToken。在 iOS 8 以及之后,注册推送服务主要分为四个步骤:
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions{if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {NSLog(@"Requesting permission for push notifications..."); // iOS 8UIUserNotificationSettings *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 *)applicationdidRegisterUserNotificationSettings:(UIUserNotificationSettings *)settings{NSLog(@"Registering device for push notifications..."); // iOS 8[application registerForRemoteNotifications];}- (void)application:(UIApplication *)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token{NSLog(@"Registration successful, bundle identifier: %@, mode: %@, device token: %@",[NSBundle.mainBundle bundleIdentifier], [self modeString], token);}- (void)application:(UIApplication *)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError *)error{NSLog(@"Failed to register: %@", error);}- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifierforRemoteNotification:(NSDictionary *)notification completionHandler:(void(^)())completionHandler{NSLog(@"Received push notification: %@, identifier: %@", notification, identifier); // iOS 8completionHandler();}- (void)application:(UIApplication *)applicationdidReceiveRemoteNotification:(NSDictionary *)notification{NSLog(@"Received push notification: %@", notification); // iOS 7 and earlier}- (NSString *)modeString{#if DEBUGreturn @"Development (sandbox)";#elsereturn @"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、义通知提示音{ "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, "ssl", "passphrase", $passphrase);$fp = stream_socket_client(//这里需要特别注意,一个是开发推送的沙箱环境,一个是发布推送的正式环境,deviceToken是不通用的"ssl://gateway.sandbox.push.apple.com:2195", $err,//"ssl://gateway.push.apple.com:2195", $err,$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;elseecho "Message successfully delivered" . PHP_EOL;fclose($fp);?>将上面的代码复制,保存成push.php
superdanny@SuperDannydeMacBook-Pro$ php push.php结果为
Connected to APNSMessage successfully delivered以上就是关于IOS推送的那些事,希望对大家的学习有所帮助。