在前台iOS中的App时获取推送通知

在前台iOS中的App时获取推送通知

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

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

正在调用,但通知屏幕中不显示通知。

我想在通知屏幕上显示通知,无论应用程序是在后台还是前台。我厌倦了寻找解决方案。任何帮助是极大的赞赏。


扬帆大鱼
浏览 1285回答 3
3回答

呼如林

要在应用程序位于前台时显示横幅消息,请使用以下方法。iOS 10,Swift 3/4:// This method will be called when app received push notifications in foregroundfunc userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {     completionHandler([.alert, .badge, .sound])}iOS 10,Swift 2.3:@available(iOS 10.0, *)func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void){     //Handle the notification     completionHandler(        [UNNotificationPresentationOptions.Alert,         UNNotificationPresentationOptions.Sound,         UNNotificationPresentationOptions.Badge])}您还必须将您的应用代表注册为通知中心的代表:import UserNotifications// snip!class AppDelegate : UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate// snip!    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {       // set the delegate in didFinishLaunchingWithOptions       UNUserNotificationCenter.current().delegate = self      ...    }

肥皂起泡泡

下面的代码将适合您:- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  {     application.applicationIconBadgeNumber = 0;                  //self.textView.text = [userInfo description];     // We can determine whether an application is launched as a result of the user tapping the action     // button or whether the notification was delivered to the already-running application by examining     // the application state.     if (application.applicationState == UIApplicationStateActive) {                         // Nothing to do if applicationState is Inactive, the iOS already displayed an alert view.                         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification" message:[NSString stringWithFormat:@"Your App name received this notification while it was running:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];         [alertView show];               }    }

人到中年有点甜

对于任何可能感兴趣的人,我最终创建了一个自定义视图,看起来像顶部的系统推送横幅,但添加了一个关闭按钮(小蓝色X)和一个选项来点击消息进行自定义操作。它还支持在用户有时间读取/解除旧通知之前到达多个通知的情况(没有限制可以堆积多少...)链接到GitHub:AGPushNote用法基本上是在线:[AGPushNoteView showWithNotificationMessage:@"John Doe sent you a message!"];它在iOS7上看起来像这样(iOS6具有iOS6的外观和感觉......)
打开App,查看更多内容
随时随地看视频慕课网APP