苹果推送服务(APNS)在应用消息推送中起着极其重要的作用。这一过程包括多个步骤和多个操作环节,既要求技术上的规范,也涉及到苹果开发者账户的相关流程,许多开发者都需要熟练掌握。
苹果推送机制原理阐述
苹果的推送系统涉及一个繁杂却井然有序的过程。当本地服务器要将信息传递给应用时,信息并不能直接抵达用户手机,而是需要先传输至苹果的推送服务器,随后该服务器再将信息发送至已安装应用的手机。这样的流程保证了消息推送的规范性和安全性。比如,国内众多应用的推送都采用了这种类似的机制。这种间接的推送方式,就好比是经历了一个严谨的安全检查程序。
苹果的APNS推送服务在接收到应用注册信息后,会反馈一个关键的token。这个token在随后的推送过程中扮演着至关重要的识别角色,缺少了它,推送信息就无法准确送达至目标设备。
接收返回Token后的操作
接收到Token后,必须将其传递给本地的Push服务器。这个过程极为关键。就好比接力赛中传递接力棒,缺少这一环节,后续的消息推送便无法进行。若在开发过程中这一步出错,消息便无法按常规流程发送。
本地服务器推送消息时,会将消息和获取的令牌一起打包,随后发送至苹果的APNS服务。这相当于推送工作的实际启动,任何小细节的失误都可能导致推送无法成功。
苹果开发者账号相关设置
在操作过程中,需要填写苹果的开发者账户信息。比如,会出现提示框要求输入账号名称。这个名称是以电子邮件地址的形式展示的。准确输入这一信息对于接下来的操作至关重要。比如在软件更新或开发特定功能时,如果账户信息填写错误,可能会造成无法与苹果的服务器进行连接的问题。
登录页面同样需要使用苹果开发者账号,可能会遇到网页跳转的情况。这种页面调整可能是为了使操作步骤或页面说明更加合理和明了,便于开发者理解和操作。
证书制作相关步骤
在证书制作的第一步,我们需要使用AppID。即便应用ID已经生成,我们仍需遵循规定步骤。比如,在挑选生成选项时,需选用之前在钥匙串访问中下载的文件。这些操作在提交审核、进行测试和支付费用等阶段都会用到。因此,严格按照苹果的规定进行操作至关重要。
openssl x509 -in aps_development.cer -inform DER -out PushChatCert.pem -outform PEM
在制作过程中,挑选苹果服务时需仔细分辨,比如明确是采用广告、游戏中心、推送通知还是付费等具体服务。这样做至关重要,因为它直接影响应用功能的完整性以及费用的计算。
配置概要文件相关
openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12
配置文件有开发版和发布版之分。创建它们的过程并不繁琐,只需挑选左侧的某个选项,再点击右上角的加号即可。创建时,需要添加设备的UUID。在XCode中可以找到这个UUID,有了它,设备才能被准确识别,并与相应的配置文件关联起来。
在创建时,文件命名要讲究方法,最好取一个能明确反映其功能的名称,便于后续管理和使用。不过,若随意取名虽无大碍,却可能给后续工作带来不便。
CONNECTED(00000003)
depth=1 /C=US/O=Entrust, Inc./OU=www.entrust.NET/rpa is incorporated by reference/OU=(c) 2009 Entrust, Inc./CN=Entrust Certification Authority - L1C
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
。。。。。(省略)
。。。。。(省略)
。。。。。(省略)
Start Time: 1416389389
Timeout : 300 (sec)
Verify return code: 0 (ok)
最后的实际操作示例
//
// AppDelegate.m
// TestPushNotifiy
//
// Created by silicon on 14-10-30.
// Copyright (c) 2014年 silicon. All rights reserved.
//
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize mainView = _mainView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
//IOS8
//创建UIUserNotificationSettings,并设置消息的显示类类型
UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:notiSettings];
} else{ // ios7
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert)];
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.mainView = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
self.window.rootViewController = self.mainView;
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken{
NSLog(@"---Token--%@", pToken);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(@"userInfo == %@",userInfo);
NSString *message = [[userInfo objectForKey:@"aps"]objectForKey:@"alert"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil nil];
[alert show];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"Regist fail%@",error);
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
在具体操作环节,比如保存文件时,必须遵循既定流程。比如,把文件存入名为“push”的文件夹,并命名为“push.p12”。在导出文件时,需要设置并验证密码。设置密码时务必小心,一旦遗忘,可能会给后续工作带来不便。文件操作过程中,还可能需要再次输入密码,每一步都应严格按流程进行。
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
//IOS8
//创建UIUserNotificationSettings,并设置消息的显示类类型
UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:notiSettings];
} else{ // ios7
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert)];
}
收到苹果服务器反馈的信息后,需将其加入本地推送服务器。此外,当设备获取苹果推送服务器的信息并触发显示推送内容的函数时,这一连串动作犹如一条生产线。举例来说,我们可通过终端进入push文件夹,输入命令来观察最终的呈现效果。
你觉得苹果推送系统的哪一部分比较难理解?欢迎在评论区留言交流。若你觉得这篇文章对你有帮助,请点赞并转发。
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken{
NSLog(@"---Token--%@", pToken);
}