显然,现在ios10可以实现:
optional func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
这个答案基本上说完成此任务所需的工具:
当您的应用处于打开状态且处于前台时,是否显示股票iOS通知横幅?
我只是不太了解如何将所有内容放在一起。
我不知道这有多重要,但是我无法保留可选的func,而xcode希望我将其切换为私有。
我正在尝试显示徽章,并且文档提供了
static var badge: UNNotificationPresentationOptions { get }
有点丢在这里。
然后我假设是否要从获取这些徽章中排除某个视图控制器,并且我没有使用导航控制器,那么我发现的这段代码行得通吗?:var window:UIWindow?
if let viewControllers = window?.rootViewController?.childViewControllers {
for viewController in viewControllers {
if viewController.isKindOfClass(MyViewControllerClass) {
print("Found it!!!")
}
}
}
有一种委托方法可在iOS 10中打开应用程序时显示通知。您必须实现此方法,才能在应用程序打开时使丰富的通知正常工作。
extension ViewController: UNUserNotificationCenterDelegate {
//for displaying notification when app is in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//If you don't want to show notification when app is open, do something here else and make a return here.
//Even you you don't implement this delegate method, you will not see the notification on the specified controller. So, you have to implement this delegate and make sure the below line execute. i.e. completionHandler.
completionHandler([.alert, .badge, .sound])
}
// For handling tap and user actions
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
html" target="_blank">switch response.actionIdentifier {
case "action1":
print("Action First Tapped")
case "action2":
print("Action Second Tapped")
default:
break
}
completionHandler()
}
}
为了在iOS 10中安排通知并提供徽章
override func viewDidLoad() {
super.viewDidLoad()
// set UNUserNotificationCenter delegate to self
UNUserNotificationCenter.current().delegate = self
scheduleNotifications()
}
func scheduleNotifications() {
let content = UNMutableNotificationContent()
let requestIdentifier = "rajanNotification"
content.badge = 1
content.title = "This is a rich notification"
content.subtitle = "Hello there, I am Rajan Maheshwari"
content.body = "Hello body"
content.categoryIdentifier = "actionCategory"
content.sound = UNNotificationSound.default
// If you want to attach any image to show in local notification
let url = Bundle.main.url(forResource: "notificationImage", withExtension: ".jpg")
do {
let attachment = try? UNNotificationAttachment(identifier: requestIdentifier, url: url!, options: nil)
content.attachments = [attachment!]
}
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 3.0, repeats: false)
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error:Error?) in
if error != nil {
print(error?.localizedDescription ?? "some unknown error")
}
print("Notification Register Success")
}
}
为了在AppDelegate中注册,我们必须在 didFinishLaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
registerForRichNotifications()
return true
}
我也在这里定义了动作。您可以跳过它们
func registerForRichNotifications() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound]) { (granted:Bool, error:Error?) in
if error != nil {
print(error?.localizedDescription)
}
if granted {
print("Permission granted")
} else {
print("Permission not granted")
}
}
//actions defination
let action1 = UNNotificationAction(identifier: "action1", title: "Action First", options: [.foreground])
let action2 = UNNotificationAction(identifier: "action2", title: "Action Second", options: [.foreground])
let category = UNNotificationCategory(identifier: "actionCategory", actions: [action1,action2], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
}
如果希望将通知标题显示在整个应用程序中的任何位置,则可以编写UNUserNotificationDelegate
in
的委托AppDelegate
并使UNUserNotificationCenter
当前委托成为AppDelegate
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print(response.notification.request.content.userInfo)
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
}
检查此链接以获取更多详细信息
Github示例
https://github.com/kenechilearnscode/UserNotificationsTutorial
这是输出
我想在通知屏幕上显示通知,无论应用程序是在后台还是前台。我为寻找解决办法而疲惫不堪。非常感谢任何帮助。
当应用程序在前台时不接收推送通知,但当应用程序在后台时接收我已遵循来自https://medium . com/@ ankushaggarwal/GCM-setup-for-Android-push-notifications-656 cf DD 8 adbd的FCM教程
和分别用于: > 当应用程序在后台时用户点击通知时处理 当手机在前台时收到通知时进行处理。 但是,当应用程序在前台时,我们如何处理用户点击通知的事件?(例如,在点击通知时将用户带到相关的视图控制器)
我已经集成了Firebase,用于在flutter中推送通知。如果应用程序在前台,我已经显示了一个通知详细信息的对话框。我收到通知正确的Android。同样在iOS通知在后台和设备锁定时工作正常。我还启用了推送通知和后台抓取。对此有什么解决办法吗?有人面临过这样的问题吗? 我使用这个Firebase插件https://pub.dev/packages/firebase_messaging
我正在使用火力点通知。当应用程序处于前台时,onMessageReceived将被调用,通知与图像一起可见(BigPictureStyle通知)。但是,当应用程序处于Kilded状态时,onMessageReceived将不被调用,而是调用带有标题和描述的基本通知。 我得到的基本通知与标题和主体时,应用程序被杀的状态。当应用程序处于死亡状态时,我如何获取图像???