一、證書處理java
測試證書是否正確:openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert apns-dev-cert.pem -key apns-dev-key.pemweb
二、APP處理spring
#pragma mark - 申請通知權限 // 申請通知權限 - (void)replyPushNotificationAuthorization:(UIApplication *)application{ if (IOS10_OR_LATER) { //iOS 10 later UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; //必須寫代理,否則沒法監聽通知的接收與點擊事件 center.delegate =self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (!error && granted) { //用戶點擊容許 NSLog(@"註冊成功"); }else{ //用戶點擊不容許 NSLog(@"註冊失敗"); } }]; // 能夠經過 getNotificationSettingsWithCompletionHandler 獲取權限設置 //以前註冊推送服務,用戶點擊了贊成仍是不一樣意,以及用戶以後又作了怎樣的更改咱們都無從得知,如今 apple 開放了這個 API,咱們能夠直接獲取到用戶的設定信息了。注意UNNotificationSettings是隻讀對象哦,不能直接修改! [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { NSLog(@"========%@",settings); }]; }else if (IOS8_OR_LATER){ //iOS 8 - iOS 10系統 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [application registerUserNotificationSettings:settings]; }else{ //iOS 8.0系統如下 [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound]; } //註冊遠端消息通知獲取device token [application registerForRemoteNotifications]; }
#pragma mark - 獲取device Token //獲取DeviceToken成功 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ //正確寫法 NSString *deviceString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; deviceString = [deviceString stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"deviceToken===========%@",deviceString); //將deviceToken發送給服務器 [self initNetworkState:[NSString stringWithFormat:@"%@",deviceToken]]; } #pragma mark 獲取DeviceToken失敗 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ NSLog(@"[DeviceToken Error]:%@\n",error.description); } #pragma mark 把deviceToken發送給服務器 -(void)initNetworkState:(NSString *)pToken{ //處理把deviceToken提交給服務器端 }
#pragma mark - 處理推送的消息內容 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ NSLog(@"%@", userInfo); self.urlStr = [userInfo valueForKey:@"link"]; self.message = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"]; [UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] intValue]; //注意:在打開應用的時候,是須要一個彈框提醒的,否則用戶看不到推送消息 if (application.applicationState == UIApplicationStateActive) { if (self.urlStr.length > 0) { // 處理推送消息 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"通知" message:@"個人信息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil]; [alert show]; }else{ UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"通知" message:@"個人信息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil]; [alert show]; } } }
三、後端推送json
package com.szqws.crm.api.front; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSONObject; import javapns.back.PushNotificationManager; import javapns.back.SSLConnectionHelper; import javapns.data.PayLoad; @Controller @CrossOrigin // 支跨域使用 @RequestMapping("/api") public class pashAPI { @RequestMapping(value = "push", method = RequestMethod.GET) @ResponseBody public JSONObject push() throws Exception { try { JSONObject jsonObject = new JSONObject(); // 從客戶端獲取的deviceToken,在此爲了測試簡單,寫固定的一個測試設備標識。 String deviceToken = "063b6ed99282c6d737c92b41e483629e2df81c2b5e64de542655a7f25b6854e7"; System.out.println("Push Start deviceToken:" + deviceToken); // 定義消息模式 PayLoad payLoad = new PayLoad(); payLoad.addAlert("this is test!"); payLoad.addBadge(1);// 消息推送標記數,小紅圈中顯示的數字。 payLoad.addSound("default"); // 註冊deviceToken PushNotificationManager pushManager = PushNotificationManager.getInstance(); pushManager.addDevice("iPhone", deviceToken); // 鏈接APNS String host = "gateway.sandbox.push.apple.com"; // String host = "gateway.push.apple.com"; int port = 2195; String certificatePath = "/Users/luoxiaodong/Desktop/apns-dev-cert.p12";// 前面生成的用於JAVA後臺鏈接APNS服務的*.p12文件位置 String certificatePassword = "szqws2019";// p12文件密碼。 pushManager.initializeConnection(host, port, certificatePath, certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12); // 發送推送 javapns.data.Device client = pushManager.getDevice("iPhone"); System.out.println("推送消息: " + client.getToken() + "\n" + payLoad.toString() + " "); pushManager.sendNotification(client, payLoad); // 中止鏈接APNS pushManager.stopConnection(); // 刪除deviceToken pushManager.removeDevice("iPhone"); System.out.println("Push End"); return jsonObject; } catch (Exception e) { e.printStackTrace(); return null; } } }