使用Swift在iOS中实现推送通知
在本文中,我们将使用Swift在iOS中实现推送通知 。 实际上,此过程比以前更容易,仅需执行少量步骤:
- 在您的iOS项目中激活推送通知
- 请求用户通知权限
- 在Apple Push Notifications服务中注册
- 发送测试通知
- 在您的应用程序中处理通知
取得Apple开发人员帐户
首先,您需要一个Apple Developer Account来访问Apple Push Notifications服务 。 但是,如果您需要首次创建一个帐户,则可以在本帖子中看到有关如何在App Store中发布iOS应用程序的信息。
在您的iOS项目中激活推送通知
- 打开应用程序项目属性 ( 项目中的根图标)
- 通常,定义一个唯一的“ Application Bundle”名称。 例如,
me.developer.ios.notifications
。 - 在“ 功能”部分中,激活“推送通知”。 结果,您的App ID将自动在App Store中注册 。
请求用户通知权限
要在应用程序中请求用户通知的权限,您需要向ViewController添加一些额外的方法。 您可以将其添加为单独的扩展,以使其可重复用于其他项目:
之后,您还将获得有关此过程的一些控制台输出:
授予的通知:true
用户通知设置:
我的设备令牌: 930976dfb191c5085b72aa347ad91c561a6fcdaab639c173a015f0745ee11401
在Apple Push Notifications服务中注册
创建身份验证密钥
首先,您需要转到Apple Developer帐户门户,并为您的应用程序创建一个身份验证密钥:
- 在键->全部下,您可以按[+]按钮创建一个新的键。
- 输入您的应用程序密钥的名称。 例如, PushNotificationsKey 。
- 保存并确认您的密钥
- 将备份下载到您的计算机中
要测试此代码,您需要使用其他Scheme启动您的应用程序。 换句话说,此代码仅在应用程序启动时执行。 因此,您需要将“运行方案”更改为“等待”:
- 在Xcode中,转到产品->方案->编辑方案…
- 将运行方案更改为“等待可执行文件启动”
- 保存您的首选项,然后再次运行
- 这次,应用程序只会编译并等待您激活
- 使用PushNotifications应用程序使用以下代码启动新的通知 :
- {“ aps”:{“警报”:“ 4个新通知!”,“声音”:“默认”,“ link_url”:“ https://developerhowto.com/”,“类别”:“通知”,“徽章” ”:4}}
- 单击手机上的通知,应用程序将启动并显示警报。
执行期间处理通知
第一部分仅适用于先前已停止的应用程序 。 但是,如果应用程序正在运行,则它需要额外的代码才能捕获通知。
更改ViewController
代码以匹配两种情况:
覆盖func viewDidAppear(_动画:布尔){
如果让aps = initialNotification {
processNotification(aps:aps) }
}
func processNotification(aps:[String:AnyObject]){
let alert = UIAlertController(title:aps [“ category”] as?字符串,消息:aps [“ alert”] as?String,preferredStyle:.alert)
alert.addAction(UIAlertAction(title:“ Ok”,样式:UIAlertAction.Style.default,处理程序:nil))
self.present(警告,动画:true,完成:无)
}
向AppDelegate
扩展添加新方法,以在运行时捕获通知:
功能应用程序(
_应用程序:UIApplication,
didReceiveRemoteNotification userInfo:[AnyHashable:Any],
fetchCompletionHandlercompleteHandler:
@转义(UIBackgroundFetchResult)->无效
){
警卫让aps = userInfo [“ aps”]为? [String:AnyObject]其他{
completeHandler(.failed)
返回
}
让main = window?.rootViewController为! ViewController
main.processNotification(aps:aps)
}
您可以使用UserNotification
框架触发本地通知 。 首先,我们需要创建一些其他方法来轻松创建和管理本地通知:
扩展UIViewController { func userNotification(message:String,title:String,count:Int) {
//创建通知内容
让内容= UNMutableNotificationContent()
content.title =标题
content.subtitle =“”
content.body =消息
content.badge =计数为NSNumber
//获取通知触发器
让触发器= UNTimeIntervalNotificationTrigger(timeInterval:5,重复:false)
//获取通知请求
let request = UNNotificationRequest(标识符:“ SimplifiedIOSNotification”,内容:content,触发器:trigger)
//将通知添加到通知中心
UNUserNotificationCenter.current()。add(request,withCompletionHandler:nil)
}
func clearNotification() {
UIApplication.shared.applicationIconBadgeNumber = 0
让通知= UNUserNotificationCenter.current()
notifications.removeAllPendingNotificationRequests()
notitications.removeAllDeliveredNotifications()
}
}
第一种方法是使用视图中的简单调用来创建新的本地通知 :
userNotification ( message : "You have 2 new messages", title : "Messages", count : 2)
第二种方法clearNotification()
,将在用户打开应用程序时清除通知标志并消除以前的通知。
完整的项目样本代码
您可以从此GitHub项目https://github.com/fraigo/ios-push-notifications下载,克隆或分叉代码示例和项目本身
享受您的通知!
注意 : 这篇文章最初发布在 https://developerhowto.com/2018/12/07/implement-push-notifications-in-ios-with-swift/