具有Java Spring框架的Apple Push Notification

我正在实现一个Java Spring Framework服务器,该服务器使用Swift将Apple Push Notification发送到iPhone。 这是您将需要的东西:

1.帐户设置

假设您已经拥有一个带有证书的Apple开发人员帐户,登录https://developer.apple.com/并转到“ 标识符 ”选项卡,然后单击“ + ”添加一个新帐户。 填写前缀和后缀,然后记得选中“ Push Notifications ”框

然后选择Edit ,然后滚动到Push Notifications部分 ,您将看到“ Configurable ”橙色指示器。 使用您的CSR文件创建并下载开发SSL证书。 双击运行并添加到您的钥匙链。

打开KeyChain Access ,找到证书并将其导出为.p2文件。 系统将提示您输入密码,最好不要将其留空。 否则,Java Spring应用程序可能无法解析空字符串。 保存此文件,该文件将在以后的步骤中使用。

2. Xcode设置

创建一个新的Xcode项目,例如Single View Application。 在“功能”选项卡中,使用您的Apple ID帐户登录启用“ 推送通知 ”。

在AppDelegate.swift中,添加一种创建设置实例的方法,该设置将在应用启动时通过提示获得用户的许可:

  func registerForPushNotifications(application:UIApplication){ 
让notificationSettings = UIUserNotificationSettings(
forTypes:[。Badge,.Sound,.Alert],类别:无)
application.registerUserNotificationSettings(notificationSettings)
}

启动完成后调用它:

  func应用程序(应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?)-> Bool { 
//应用程序启动后进行自定义的替代点。
registerForPushNotifications(应用程序)
返回真
}

在同一AppDelegate.swift文件中,添加以下方法,该方法将在用户接受或拒绝权限时进行处理。

  func应用程序(应用程序:UIApplication,didRegisterUserNotificationSettings notificationSettings:UIUserNotificationSettings){ 
如果notificationSettings.types!=。无{
application.registerForRemoteNotifications()
}
}

如果注册成功,请添加以下方法来处理:

  func应用程序(应用程序:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:NSData){ 
让tokenChars = UnsafePointer (deviceToken.bytes)
var tokenString =“”

对于0中的i。.<deviceToken.length {
tokenString + =字符串(格式:“%02.2hhx”,参数:[tokenChars [i]])
}

打印(“设备令牌:”,tokenString)
}

同样对于注册失败的情况:

  func应用程序(应用程序:UIApplication,didFailToRegisterForRemoteNotificationsWithError错误:NSError){ 
打印(“无法注册:”,错误)
}

您现在可以尝试运行该应用程序。 请注意,您将需要物理设备而不是模拟器来对其进行测试。 如果一切顺利,您应该在控制台中看到设备令牌。 记录下来以备后用。

3. Java Spring服务器设置

使用您喜欢的IDE(例如NetBeans或Intellij)创建Java spring框架服务器。 在此示例中,我们使用的是maven build,带有pom.xml文件,如下所示:

   
<project xmlns =” http://maven.apache.org/POM/4.0.0” xmlns:xsi =” http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation =” http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd“>
4.0.0
   com.victorleungtw  
推送通知
0.0.1-SNAPSHOT
jar
  推送通知 
推送通知
   
org.springframework.boot
spring-boot-starter-parent
1.3.5.RELEASE
<! —从资源库中查找父级→
   
UTF-8
1.8
   

org.springframework.boot
spring-boot-starter-data-rest



org.springframework.boot
spring-boot-starter-test
测试



org.springframework.restdocs
spring-restdocs-mockmvc
测试







org.springframework.boot
spring-boot-maven-plugin


   

然后,我们利用Maven存储库中的notnoop库。 将此依赖项添加到pom.xml

   
com.notnoop.apns
apns
1.0.0.Beta6

服务器启动时,它将寻找主服务器,并在下面提供示例PushNotificationApplication.java

 软件包com.victorleungtw; 
 导入org.springframework.boot.SpringApplication; 
导入org.springframework.boot.autoconfigure.SpringBootApplication;
  @SpringBootApplication 
公共类PushNotificationApplication {
 公共静态void main(String [] args){ 
SpringApplication.run(PushNotificationApplication.class,args);
}
}

作为一个简单的演示,我们将创建一个NotificationController.java

 软件包com.victorleungtw; 
 导入java.util.Map; 
导入java.util.Date;
 导入java.util.concurrent.atomic.AtomicLong; 
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestParam;
导入org.springframework.web.bind.annotation.RestController;
 导入com.notnoop.apns.APNS; 
导入com.notnoop.apns.ApnsService;
  / ** 
*
* @作者victorleungtw
* /
@RestController
公共类NotificationController {
私有静态最终String模板=“您好,%s!”;
私人最终AtomicLong计数器= new AtomicLong();
ApnsService服务= null;
  @RequestMapping(“ / notification”) 
public Notification notification(@RequestParam(value =“ name”,defaultValue =“ World”)字符串名称){

System.out.println(“正在发送iOS推送通知...”);

ApnsService服务= APNS.newService()
.withCert(“ XXX.p12 ”,“ YOUR_PASSWORD ”)
.withSandboxDestination()
。建立();

字符串有效负载= APNS.newPayload()
.alertBody(“再简单不过了!”)
.alertTitle(“测试警报标题”).build();

字符串标记=“ b8f3b2513caa ..... ”;

System.out.println(“ payload:” + payload);

service.push(令牌,有效载荷);

System.out.println(“希望已发送消息...”);

返回新的Notification(counter.incrementAndGet(),
String.format(template,name));
}
}

将“ XXX.p12”,“ YOUR_PASSWORD”和“ b8f3b2513caa…..”分别替换为您的实际.p12文件路径,密码和设备令牌。 如果不确定文件路径,则可以将.p12文件放在与/ src文件夹(即项目文件夹的根目录)相同的级别。 而已。 运行以下命令:

  mvn安装 

并启动服务器:

  mvn spring-boot:运行 

打开浏览器,点击路由http:// localhost:8080 / notification,您应该能够收到通知!

让我知道你是否有任何问题🙂