客户端(Objective - C 项目)
- 导入必要框架:在项目中导入
UserNotifications.framework
框架。如果是旧版本系统,还需导入UIKit.framework
用于处理推送相关的委托方法。
- 请求推送权限:在应用启动时,在
AppDelegate
的application:didFinishLaunchingWithOptions:
方法中请求推送权限。例如:
if (@available(iOS 10.0, *)) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
}];
} else {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
- 注册设备令牌:实现
AppDelegate
的application:didRegisterForRemoteNotificationsWithDeviceToken:
方法,获取设备令牌并将其发送到服务器。例如:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"Device Token: %@", token);
// 将token发送到服务器
}
- 处理推送通知:
- 前台收到通知:如果应用在前台收到推送通知,实现
AppDelegate
的application:didReceiveRemoteNotification:fetchCompletionHandler:
方法(iOS 7.0+)或application:didReceiveRemoteNotification:
方法(iOS 7.0 之前)。例如:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"Received remote notification: %@", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}
- **后台收到通知**:应用在后台时收到通知,用户点击通知进入应用,同样可以在`application:didReceiveRemoteNotification:fetchCompletionHandler:`方法中处理。
服务器端
- 获取证书:从苹果开发者中心创建并下载推送通知证书(Development 或 Production),将其转换为 PEM 格式。例如,使用 OpenSSL 工具:
openssl x509 -in aps_development.cer -inform DER -out ApplePushServices.pem -outform PEM
openssl pkcs12 -nocerts -out key.pem -in aps_development.p12
cat key.pem ApplePushServices.pem > Certificates.pem
- 选择推送服务库:根据服务器使用的语言选择合适的推送服务库,如 Python 的
apns2
库,Java 的javapns
库等。
- 配置服务器端代码:以
apns2
库(Python)为例,基本代码如下:
from apns2.client import APNsClient
from apns2.payload import Payload
# 证书路径
certificate = 'Certificates.pem'
# 设备令牌
device_token = 'your_device_token'
# 创建客户端
client = APNsClient(certificate, use_sandbox=True)
# 创建推送负载
payload = Payload(alert="Hello, this is a push notification!", sound="default", badge=1)
# 发送推送
futures = client.send_notification(device_token, payload)
- 处理反馈:服务器需要定期从苹果反馈服务获取无效的设备令牌,以便从数据库中移除,避免向无效设备发送推送。不同语言的库有不同的处理反馈方式,如
apns2
库中可以通过client.get_inactive_devices()
获取无效设备令牌列表。