首页 文章

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

提问于
浏览
140

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

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

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

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

14 回答

  • 115

    将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])
    }
    
  • 9

    如果应用程序在前台运行,则iOS在设计中赢得了't show a notification banner/alert. That' . 但是我们可以通过如下使用_456116来实现它

    • 在接收遥控器时检查应用程序是否处于活动状态
      通知 . 如果处于活动状态,则触发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)
    }
    
  • -2

    根据apple documentation,是的,您可以在应用运行时显示通知
    enter image description here

  • 51

    正如@Danial Martine所说,iOS不会显示通知 Banner /警报 . 这是设计的 . 但如果真的必须这样做,那么有一种方法 . 我也是这样做的 .

    1.从Parse FrameWork下载解析框架工作

    2.进口 #import <Parse/Parse.h>

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

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

    PFPush将注意如何处理远程通知 . 如果App位于前台,则会显示警报,否则会在顶部显示通知 .

  • 12

    以下是app处于活动状态(前台或打开)时接收推送通知的代码 . UNUserNotificationCenter documentation

    @available(iOS 10.0, *)
    func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
    {
         completionHandler([UNNotificationPresentationOptions.Alert,UNNotificationPresentationOptions.Sound,UNNotificationPresentationOptions.Badge])
    }
    

    如果您需要访问userInfo的通知使用代码: notification.request.content.userInfo

  • 40

    Objective C

    enter image description here

    对于 iOS 10 ,我们需要在 foreground 中集成 willPresentNotification 方法用于显示通知 Banner .

    如果app处于前台模式(活动)

    - (void)userNotificationCenter:(UNUserNotificationCenter* )center willPresentNotification:(UNNotification* )notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
        NSLog( @“Here handle push notification in foreground" ); 
        //For notification Banner - when app in foreground
    
        completionHandler(UNNotificationPresentationOptionAlert);
    
        // Print Notification info
        NSLog(@"Userinfo %@",notification.request.content.userInfo);
    }
    
  • -2

    如果应用程序在前台运行,则iOS在设计上赢得了't show a notification banner/alert. That' . 您必须编写一些代码来处理应用程序在前台接收通知时的情况 . 您应该以最合适的方式显示通知(例如,将徽章编号添加到 UITabBar 图标,模拟通知中心 Banner 等) .

  • 2

    如上所述,您应该使用 UserNotification.framework 来实现此目的 . 但是出于我的目的,无论如何我必须在应用程序中显示它并希望有 iOS 11 样式,所以我创建了一个小帮助器视图,可能对某人有用 .

    GitHub iOS 11 Push Notification View .

  • 0

    对于任何可能感兴趣的人,我最终创建了一个自定义视图,看起来像顶部的系统推送 Banner ,但添加了一个关闭按钮(小蓝色X)和一个选项来点击消息进行自定义操作 . 它还支持在用户有时间读取/解除旧通知之前到达多个通知的情况(没有限制可以堆积多少...)

    链接到GitHub:AGPushNote

    The usage is basically on-liner:

    [AGPushNoteView showWithNotificationMessage:@"John Doe sent you a message!"];
    

    在iOS7上它看起来像这样(iOS6具有iOS6的外观和感觉...)

    enter image description here

  • 1

    要在应用程序位于前台时显示 Banner 消息,请使用以下方法 .

    iOS 10, Swift 3/4

    // This method will be called when app received push notifications in foreground
    func 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!
    
    // set the delegate in didFinishLaunchingWithOptions
    UNUserNotificationCenter.current().delegate = self
    
  • 6

    如果您的应用程序处于前台状态,则表示您当前正在使用相同的应用程序 . 所以一般不需要在顶部显示通知 .

    但是,如果你想在这种情况下显示通知,你必须创建自定义警报视图或自定义视图,如Toast或其他东西,以向用户显示您已收到通知 .

    如果您的应用中有此类功能,您还可以在顶部显示徽章 .

  • 17

    下面的代码将适合您:

    - (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];          
        }    
    }
    
  • 25

    您可以创建自己的模仿 Banner 提醒的通知 .

    一种方法是创建一个看起来像 Banner 的自定义uiview,可以设置动画并响应触摸 . 考虑到这一点,您可以使用更多功能创建更好的 Banner .

    或者你可以找一个为你做的api,并将它们作为podfiles添加到你的项目中 .

    这是我用过的一对夫妇:

    https://github.com/terryworona/TWMessageBarManager

    https://github.com/toursprung/TSMessages

  • 7

    在你的app委托中使用下面的代码

    import UIKit
    import UserNotifications
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
     var currentToken: String?
     var window: UIWindow?
     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            application.registerForRemoteNotifications()
            let center = UNUserNotificationCenter.current()
            center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
    
                // Enable or disable features based on authorization.
                if granted == true
                {
                    print("Allow")
                    UIApplication.shared.registerForRemoteNotifications()
                }
                else
                {
                    print("Don't Allow")
                }
            }
            UNUserNotificationCenter.current().delegate = self
    
            return true
        }
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){
            let tokenParts = deviceToken.map { data -> String in
                return String(format: "%02.2hhx", data)
            }
            let token = tokenParts.joined()
            currentToken = token  //get device token to delegate variable
    
        }
     public class var shared: AppDelegate {
            return UIApplication.shared.delegate as! AppDelegate
        }
     func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
             completionHandler([.alert, .badge, .sound])
        }
    }
    

相关问题