本地环境
- MacOX 10.13.6
- XCode 10.1
- iPhone6S/iPhone6P ios11.0.3
证书配置
Certificate Signing Request
推送证书配置
1.打开ios开发者官网,Account->Certificates, Identifiers & Profiles
https://developer.apple.com/
2.Register a new identifier
勾选Notification
点击regsiter后,回到identifies页面重新点击进去创建证书
证书安装后,本地钥匙串中会生成公私钥
下载Wordwide证书,并安装到本地
-
添加device测试设备
4.add profiles
Xcode配置
- 新建app
-
配置profile
3.打开通知
4.注册通知服务,并获取设备的devicetoken
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
// 1.注册UserNotification,以获取推送通知的权限
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[application registerUserNotificationSettings:settings];
// 2.注册远程推送
[application registerForRemoteNotifications];
}
return YES;
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
NSLog(@"deviceToken=====%@",deviceToken);
}
5.真机调试,并获取devicetoken
消息推送
pem证书推送
- pem证书生成
略 - 推送
curl -v -d \
'{"aps":{alert":"test", "mutable-content":1}}' \
-H "apns-topic: com.liuyun.pigeon" -H "apns-priority: 10" \
--http2 \
--cert /Users/apple/Desktop/pigeon/pigeon.pem:password https://api.development.push.apple.com/3/device/devicetokenxxxxx
p12证书推送
p12导出
<dependency>
<groupId>com.eatthepath</groupId>
<artifactId>pushy</artifactId>
<version>0.14.0</version>
</dependency>
public static void sendWithP12(){
SimpleApnsPushNotification pushNotification;
try {
final ApnsClient apnsClient = new ApnsClientBuilder()
.setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)
.setClientCredentials(new File(certificatePath), certificatePassword)
.build();
ApnsPayloadBuilder payloadBuilder = new SimpleApnsPayloadBuilder();
payloadBuilder.setAlertTitle("new message");
payloadBuilder.setBadgeNumber(1);
payloadBuilder.setAlertBody("this is a test push notification");
payloadBuilder.setContentAvailable(true);
String payload = payloadBuilder.build();
String token = TokenUtil.sanitizeTokenString(deviceToken3);
pushNotification = new SimpleApnsPushNotification(token, "com.liuyun.pigeon", payload);
apnsClient.sendNotification(pushNotification);
apnsClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
p8推送
ios开发者后台添加key,并下载p8秘钥,一次性下载,请妥善保存。
public static void pushWithP8() {
try {
ApnsClient apnsClient = new ApnsClientBuilder()
.setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)
.setSigningKey(ApnsSigningKey.loadFromPkcs8File(
new File("/Users/apple/Desktop/pigeon/xxxxx.p8"), "xxxxx", "xxxxx"))
.build();
final SimpleApnsPushNotification pushNotification;
{
final ApnsPayloadBuilder payloadBuilder = new SimpleApnsPayloadBuilder();
payloadBuilder.setAlertBody("This is a test message");
final String payload = payloadBuilder.build();
final String token = TokenUtil.sanitizeTokenString("xxx token xxxxxxxxxx");
pushNotification = new SimpleApnsPushNotification(token, "com.liuyun.pigeon", payload);
}
apnsClient.sendNotification(pushNotification);
} catch (Exception e) {
e.printStackTrace();
}
}