[英]How to handle push notification in swift?
我有一个 ios 应用程序,我在其中使用 apns 发送推送。 我需要处理推送消息,如果正确则显示消息。 我如何在swift中实现它? 这是我的代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions:
[NSObject: AnyObject]?) -> Bool {
registerForPushNotifications(application)
return true
}
func registerForPushNotifications(application: UIApplication) {
let notificationSettings = UIUserNotificationSettings(
forTypes: [.Badge, .Sound, .Alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types != .None {
application.registerForRemoteNotifications()
}
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
var tokenString = ""
for i in 0..<deviceToken.length {
tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}
print("Device Token:", tokenString)
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Failed to register:", error)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
}
func applicationDidBecomeActive(application: UIApplication) {
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
}
在什么 function 我可以在那里处理推送通知?
到达你的iOS应用程序时处理推送通知是一个棘手的部分,以充分利用它。
当推送通知到达时,您的应用程序可以进入
关闭状态 - 应用程序被杀死,运行状态 - 应用程序处于前台,暂停状态 - 应用程序处于后台 ,
到达时处理推送通知
让我们逐一讨论如何在每个州处理它们
封闭状态:
当应用程序关闭(某些其他应用程序正在运行或手机被锁定)时,推送通知到达,您点击它以打开应用程序。 将对appDelegate's
方法进行控制,即didFinishLaunchingWithOptions
:请注意,当您通常通过从手机中点按其图标来启动应用程序时。 didFinishLaunchingWithOptions:
首先使用launchOptions == nil调用。 如果您通过单击收到的推送通知启动应用程序,则执行didFinishLaunchingWithOptions:使用其launchOptions!=nil
调用。 关键点如果你想在点击推送通知启动应用程序时做一些特别的事情,你需要在didFinishLaunchingWithOptions
添加代码:
像这样
if (launchOptions != nil)
{
//do some thing special e.g. display particular ViewController or Set some notification badge value.
}
运行状态
如果您的应用程序正在运行(在前台)并且收到推送通知,则屏幕上将不显示与该通知相关的任何内容 - 没有警报,没有消息,没有声音。 而是将调用appDelegate的跟随方法
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
}
您可以根据需要实现此方法,以便您希望如何响应通知。
暂停状态
如果您的应用程序处于后台(手机已锁定或某些其他应用程序正在运行)并且已收到推送通知,则会显示声音通知,并且点击该通知应用程序将启动以下appDelegate方法被调用
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
}
与在运行状态下收到通知时调用的相同。 请注意,通过使用UIApplication的applicationState
属性,您始终可以在此方法中找到您的应用是否从后台状态唤醒。 在这种情况下,您可以通过推送通知从后台打开应用程序时执行一些特殊操作。
根据你的问题,
一旦在设备上收到通知,您就无法切换通知的可见性。
这种功能/功能仅适用于Android。 在收到通知时,Android开发人员可以决定是否呈现视图。
所以尝试从服务器端控制它。
我想举一个关于处理远程推送通知的例子。
所有将在 AppDelegate.swift 文件中;
首先从这个 function 注册后获取您的令牌,以便可以从您的服务器或提供商发送远程推送通知 (RPM):
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Send device token that accepted to retrieve remote notifications to server
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
NSLog("APNs registration success, device token: \(token)")
}
检查didFinishLaunchWithOptions
方法中的启动选项中是否存在远程通知信息,以了解是否通过点击通知警报启动了应用程序,如下所示:
// Check if there any user info about remote notifications in 'launchOptions'
/// Get user your data from userInfo dictionary if you need
if let userInfo = launchOptions?[.remoteNotification] as? [String: AnyObject],
let exmplValue = userInfo["yourValue"] as? String {
NSLog("[AppDelegate] - Info: App launched by tapping notification alert. We have new value: \(exmplValue)")
/// Here you can change rootVC or storing some values to process it in your mainVC etc.
}
然后像下面那样实现didReceiveRemoteNotification
function 以处理 state 活动和非活动中的推送通知:
/// This function made for handling push notifications when app running (both states active or inactive)
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
/// Hide badge number if it exist
application.applicationIconBadgeNumber = 0
/// Get user your data from userInfo dictionary if you need
guard let aps = userInfo["aps"] as? [String: Any],
let alert = aps["alert"] as? [String: Any],
let title = alert["title"] as? String,
let body = alert["body"] as? String,
let exampleValue = userInfo["example"] as? String else {
NSLog("[AppDelegate] - Warning: user info missing aps or other data.")
return
}
/// Check current application state
if UIApplication.shared.applicationState == .active {
// Your app in foreground, you can ask user to want to take action about what you want to do.
} else {
// Your app in background at most in inactive mode, user when taps on notification alert app will become active so you can do whatever you want directly
}
}
奖金:
在您的 AppDelegate.swift 文件中实施UNUserNotificationCenterDelegate
,然后在didFinishLaunchWithOptions
确认通知中心的委托,如下所示: UNUserNotificationCenter.current().delegate = self
最后覆盖此 function 以调整将出现的通知的徽章、声音和警报。
// Assign delegate to show LPN or RPN badge, alert, sound
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
/// I would like to notify user by sound only when app is active
/// otherwise show notification (alert, badge, sound)
if UIApplication.shared.applicationState == .active {
completionHandler(.sound)
} else {
completionHandler([.alert, .badge, .sound])
}
}
请记住,在这些示例中,我没有实施静默(后台)通知,因为当我激活静默通知时,我的应用程序处理 function 以下的所有类型的通知,我无法理解我的应用程序是通过点击 RPM 或其他方式启动的,暂时不熟悉对于这种情况,如果您需要它,您必须添加后台模式功能并在您的目标中检查远程通知 -> 您的应用程序 -> 签名和功能,然后实施此 function 来处理 RPM:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
<#code#>
}
快乐编码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.