PushNotification 横幅未在 ios 10.3 中显示
Posted
技术标签:
【中文标题】PushNotification 横幅未在 ios 10.3 中显示【英文标题】:PushNotification banner not showing in ios 10.3 【发布时间】:2017-11-14 06:06:39 【问题描述】:我已将推送通知集成到我的项目中,出于某种原因,横幅会在我处于后台状态时出现,而不是在我处于前台时出现。我确定我错过了一些东西,我的代码如下。任何帮助将不胜感激。
import UserNotifications
class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
let center = UNUserNotificationCenter.current()
center.delegate = self //not sure this is appropriate
registerForPushNotifications()
func registerForPushNotifications()
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge])
(granted, error) in
guard granted else return
self.getNotificationSettings()
func getNotificationSettings()
UNUserNotificationCenter.current().getNotificationSettings (settings) in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else return
DispatchQueue.main.async(execute:
UIApplication.shared.registerForRemoteNotifications()
)
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
let tokenParts = deviceToken.map data -> String in
return String(format: "%02.2hhx", data)
let token = tokenParts.joined()
if let valueInDB = UserDefaults.standard.value(forKey: "Permission_Granted")
UserDefaults.standard.setValue(token, forKey: "PUSH_TOKEN")
UserDefaults.standard.synchronize()
print("Device Push Token: \(token)")
每次我收到通知时都会调用它,不管它是在前台还是后台运行。因此批处理图标得到更新。
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
print("Recived: \(userInfo)")
completionHandler(.newData)
self.onPushRecieved = MP3ViewController().catchPushNotifications
let notification = JSON(userInfo)
print(notification)
let mediaId = (notification["media_id"]).stringValue
print(mediaId)
var badgeCount = 0
var pushAvailable = false
if let pushCount = UserDefaults.standard.value(forKey: "PUSH_COUNT")
badgeCount = pushCount as! Int
print(badgeCount)
badgeCount = badgeCount + 1
UIApplication.shared.applicationIconBadgeNumber = badgeCount
print(badgeCount)
UserDefaults.standard.setValue(badgeCount, forKey: "PUSH_COUNT")
UserDefaults.standard.synchronize()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
print(isOnBackGround)
if isOnBackGround
if mediaId != nil
DispatchQueue.main.async
let mP3ViewController = storyboard.instantiateViewController(withIdentifier:"MP3ViewController") as! MP3ViewController
mP3ViewController.media_ID = mediaId
self.navigationController?.pushViewController(mP3ViewController, animated:true)
self.isOnBackGround = false
else
print("Running on foreground")
【问题讨论】:
UNUserNotificationCenterDelegate 协议定义了接收通知和处理动作的方法。那么处理自定义横幅的方法是什么 @danuUNUserNotificationCenterDelegate
的 delegate
方法在哪里?
【参考方案1】:
您可能希望实现以下功能,例如:
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void)
completionHandler([.badge, .alert, .sound])
此代表的:UNUserNotificationCenterDelegate
【讨论】:
以上是关于PushNotification 横幅未在 ios 10.3 中显示的主要内容,如果未能解决你的问题,请参考以下文章
amp 应用程序横幅未在 android 和 IOS 设备中正常显示
Mobilefirst PushNotification:在服务器上使用 wl_anonymousUserRealm 时,应用程序未在单击通知时调用 onReceive 方法