问题描述
我正在尝试为我的应用程序设置推送通知系统.我有一个服务器和一个开发人员许可证来设置推送通知服务.
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申请
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的清单:
So the checklist of APNS:
- 创建推送通知允许的AppId
- 使用有效的证书和应用ID创建SSL证书
- 使用相同的证书创建配置文件,并确保在沙箱(开发配置)的情况下添加设备
注意: ,如果在SSL证书后创建配置文件,那会很好.
Note: That will be good if Create Provisioning profile after SSL Certificate.
使用代码:
With Code:
- 注册用于推送通知的应用
- 处理didRegisterForRemoteNotificationsWithDeviceToken方法
- 设置目标>功能>后台模式>远程通知
- 处理didReceiveRemoteNotification
这篇关于如何在Swift中设置推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!