如何在 Swift 中设置推送通知 [英] How to set up push notifications in Swift
问题描述
我正在尝试为我的应用程序设置推送通知系统.我有一个服务器和一个开发者许可证来设置推送通知服务.
I am trying to set up a push notification system for my application. I have a server and a developer license to set up the push notification service.
我目前正在 Swift 中运行我的应用.我希望能够从我的服务器远程发送通知.我该怎么做?
I am currently running my app in Swift. I would like to be able to send the notifications remotely from my server. How can I do this?
推荐答案
虽然解决了推送通知的问题,但我仍然相信立即分享集成完整案例以缓解:
While the answer is given well to handle push notification, still I believe to share integrated complete case at once to ease:
为APNS注册应用程序,(在AppDelegate.swift中的didFinishLaunchingWithOptions方法中包含以下代码)
To Register Application for APNS, (Include the following code in didFinishLaunchingWithOptions method inside AppDelegate.swift)
IOS 9
var settings : UIUserNotificationSettings = UIUserNotificationSettings(forTypes:UIUserNotificationType.Alert|UIUserNotificationType.Sound, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
<小时>
IOS 10 之后
引入了 UserNotifications 框架:
Introduced UserNotifications framework:
导入 UserNotifications 框架并在 AppDelegate.swift 中添加 UNUserNotificationCenterDelegate
Import the UserNotifications framework and add the UNUserNotificationCenterDelegate in AppDelegate.swift
注册 APNS 应用程序
To Register Application for APNS
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
// If granted comes true you can enabled features based on authorization.
guard granted else { return }
application.registerForRemoteNotifications()
}
这将调用以下委托方法
func application(application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
//send this device token to server
}
//Called if unable to register for APNS.
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
println(error)
}
收到通知后,以下代表将调用:
On Receiving notification following delegate will call:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
println("Recived: \(userInfo)")
//Parsing userinfo:
var temp : NSDictionary = userInfo
if let info = userInfo["aps"] as? Dictionary<String, AnyObject>
{
var alertMsg = info["alert"] as! String
var alert: UIAlertView!
alert = UIAlertView(title: "", message: alertMsg, delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
}
要确定我们可以使用的权限:
To be identify the permission given we can use:
UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in
switch setttings.soundSetting{
case .enabled:
print("enabled sound")
case .disabled:
print("not allowed notifications")
case .notSupported:
print("something went wrong here")
}
}
<小时>
APNS 的清单:
- 创建允许推送通知的 AppId
- 使用有效的证书和应用程序 ID 创建 SSL 证书
- 使用相同的证书创建配置文件,并确保在沙盒(开发配置)的情况下添加设备
注意:如果在 SSL 证书之后创建配置文件会很好.
Note: That will be good if Create Provisioning profile after SSL Certificate.
使用代码:
- 注册应用以获得推送通知
- 处理 didRegisterForRemoteNotificationsWithDeviceToken 方法
- 设定目标>能力>后台模式>远程通知
- 处理 didReceiveRemoteNotification
这篇关于如何在 Swift 中设置推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!