我在我的应用程序中使用推送通知服务。当应用程序在后台时,我能够在通知屏幕上看到通知(当我们从iOS设备顶部向下滑动时显示的屏幕)。但如果应用程序是在前台的委托方法

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo

正在被调用,但通知屏幕上没有显示通知。

我想在通知屏幕上显示通知,不管应用程序是在后台还是前台。我厌倦了寻找解决办法。任何帮助都非常感激。


当前回答

100%工作测试

第一次进口

import UserNotifications

然后在类中添加委托

UNUserNotificationCenterDelegate


class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate

下面的方法是负责,而应用程序是打开和通知来。

将呈现

   @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            let content = notification.request.content

           let alertVC = UIAlertController.init(title: title, message: body, preferredStyle: .alert)

            alertVC.addAction(UIAlertAction.init(title: appLan_share.Ok_txt, style: .default, handler: {
                _ in
                   //handle tap here or navigate somewhere…..                
            }))

            vc?.present(alertVC, animated: true, completion: nil)

            print("notification Data: \(content.userInfo.values)")
                completionHandler([.alert, .sound])



}

您还可以通过检查当前应用程序状态来处理应用程序状态。

此外,如果你的应用程序没有运行,那么下面的方法是负责处理推送通知

didReceive

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        let aps = userInfo["aps"] as? [String: Any]
        let alert = aps?["alert"] as? [String: String]

}

其他回答

Swift 5

1)使用UNUserNotificationCenterDelegate确认委托到AppDelegate 2)在didFinishLaunch中UNUserNotificationCenter.current().delegate = self 3)在AppDelegate中实现如下方法。

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
     print("Push notification received in foreground.")
     completionHandler([.alert, .sound, .badge])
}

就是这样!

正如@Danial Martine所说,iOS不会显示通知横幅/提醒。这是故意的。但如果真要这么做,还有一个办法。我也用同样的方法做到了这一点。

1.从parse FrameWork下载解析框架

2.导入# Import <Parse/Parse.h> .h

3.将以下代码添加到您的didReceiveRemoteNotification方法

 - (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [PFPush handlePush:userInfo];
}

PFPush会注意如何处理远程通知。如果App在前台,这显示警报,否则它显示在顶部的通知。

如果应用程序在前台运行,iOS不会显示通知横幅/警报。这是故意的。你必须编写一些代码来处理应用程序在前台接收通知的情况。您应该以最合适的方式显示通知(例如,向UITabBar图标添加徽章号,模拟notification Center横幅等)。

添加completionHandler行委托方法为我解决了同样的问题:

//Called when a notification is delivered to a foreground app.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

completionHandler([.alert, .badge, .sound])
} 

如果应用程序在前台运行,iOS不会显示通知横幅/警报。这是故意的。但我们可以通过使用UILocalNotification实现它,如下所示

Check whether application is in active state on receiving a remote notification. If in active state fire a UILocalNotification. if (application.applicationState == UIApplicationStateActive ) { UILocalNotification *localNotification = [[UILocalNotification alloc] init]; localNotification.userInfo = userInfo; localNotification.soundName = UILocalNotificationDefaultSoundName; localNotification.alertBody = message; localNotification.fireDate = [NSDate date]; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; }

迅速:

if application.applicationState == .active {
    var localNotification = UILocalNotification()
    localNotification.userInfo = userInfo
    localNotification.soundName = UILocalNotificationDefaultSoundName
    localNotification.alertBody = message
    localNotification.fireDate = Date()
    UIApplication.shared.scheduleLocalNotification(localNotification)
}