当应用程序处于前台时如何显示本地通知

Posted

技术标签:

【中文标题】当应用程序处于前台时如何显示本地通知【英文标题】:How to show local notification when app is foreground 【发布时间】:2016-10-06 10:14:15 【问题描述】:

如何使用新的(来自 ios 10 的)通知框架 UserNotifications https://developer.apple.com/reference/usernotifications?language=objc 始终显示本地通知,即使在前台也是如此?

【问题讨论】:

添加你的语言标签 【参考方案1】:
#import <UserNotifications/UserNotifications.h>
#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

//interface
 @interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>

@end

//implementation
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    [self registerForRemoteNotifications];
    return YES;


- (void)registerForRemoteNotifications 
    if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0"))
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
             if(!error)
                 [[UIApplication sharedApplication] registerForRemoteNotifications];
             
         ];  
    
    else 
        // Code for old versions
    


////...........Handling delegate methods for UserNotifications........
//Called when a notification is delivered to a foreground app.

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
    NSLog(@"User Info : %@",notification.request.content.userInfo);
    completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);


//Called to let your app know which action was selected by the user for a given notification.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
    NSLog(@"User Info : %@",response.notification.request.content.userInfo);
    completionHandler();

希望这会对您有所帮助。 :)

【讨论】:

Sommm,你在哪里设置了前台显示通知的参数?因为看起来我做了同样的事情,但通知只在后台模式下显示。显示出现在通知栏中的 meens。 你以前用过这个解决方案吗? 应用处于活动状态时显示通知? 关键在于实现 willPresentNotification 并使用“None”以外的值调用完成处理程序。见UNUserNotificationCenterDelegate Docs【参考方案2】:
 You can do this way.  

//in Appdelegate
-(void)application:(UIApplication *)application didReceiveLocalNotification(UILocalNotification *)notification

    dispatch_async(dispatch_get_main_queue(),^

    NSLog(@"alertbody ..%@",  notification.alertBody);
    NSLog(@"title ..%@",  notification.alertTitle);
    NSLog(@"action ..%@",  notification.alertAction);
    NSLog(@"userinfo..%@",  notification.userInfo);
    [[NSNotificationCenter defaultCenter]postNotificationName:@"NotificationReceived" object:nil userInfo:notification.userInfo];
          );



//in present view controller
-(void)viewDidLoad

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificationReceivedMethod:) name:@"NotificationReceived" object:nil];


-(void) viewWillDisappear:(BOOL)animated 

 [[NSNotificationCenter defaultCenter]removeObserver:self name:@"NotificationReceived" object:nil];


-(void)notificationReceivedMethod

//create you custom popup or view

【讨论】:

不不不!我询问了 iOS 10 的 UserNotification 框架

以上是关于当应用程序处于前台时如何显示本地通知的主要内容,如果未能解决你的问题,请参考以下文章

当应用程序在 iOS 10 的前台时,不会调用“willPresent 通知”并且本地通知不会显示

iOS 10,当应用程序处于前台时显示本地通知?

如何在前台接收推送通知时显示颤振本地通知?

当应用程序处于后台/终止并收到 FCM 通知时,如何在 Flutter 中显示本地通知?

收到本地通知时无法播放音乐,当应用程序处于后台状态时

使用前台应用程序检测本地通知的点击,swift 4