繁体   English   中英

当应用程序从后台收到推送通知或在iOS中终止时,无法收听来自NSNotificationCenter的通知

[英]Unable to listen notification from NSNotificationCenter when App received push notification from background or terminated in iOS

我正在iOS 10中实现推送通知。一切正常。

但是,当APP收到推送通知时(不仅处于活动状态,而且处于后台/终止状态),我都需要点击API。

为此,我使用NSNotificationCenter来监听应用程序收到如下推送通知时的通知:

   - (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    NSDictionary *userInfo = notification.request.content.userInfo;
    NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);

    NSLog(@"%@", userInfo);

    if( [UIApplication sharedApplication].applicationState == UIApplicationStateInactive )
    {
        NSLog( @"INACTIVE" );
        completionHandler( UNNotificationPresentationOptionAlert );
    }
    else if( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground )
    {
        NSLog( @"BACKGROUND" );
        completionHandler( UNNotificationPresentationOptionAlert );
    }
    else
    {
        NSLog( @"FOREGROUND" );
        completionHandler( UNNotificationPresentationOptionAlert );
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil];

}

我像这样在ViewController.m监听此通知

    - (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable:) name:@"reloadTheTable" object:nil];
}

 - (void)reloadTable:(NSNotification *)notification
{
// Calling API here
}

当应用程序在前台运行时,这工作正常。 但不在背景和终止状态。

我是否有任何错误或需要实施的其他任何措施?

从iOS 10开始,我们必须添加UserNotifications框架并委托

所以首先我们需要在appDelegate.h中做以下事情

#import <UserNotifications/UserNotifications.h>  
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>

对于FOREGROUND状态

- (void)userNotificationCenter:(UNUserNotificationCenter *)center  
willPresentNotification:(UNNotification *)notification  
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler  
{  
  NSLog( @"Handle push from foreground" );  
  // custom code to handle push while app is in the foreground  
  NSLog(@"%@", notification.request.content.userInfo);
}   

这是背景状态

所以在这里您需要添加通知

- (void)userNotificationCenter:(UNUserNotificationCenter *)center  
 didReceiveNotificationResponse:(UNNotificationResponse *)response  
 withCompletionHandler:(void (^)())completionHandler 
{  
  NSLog( @"Handle push from background or closed" );  
 // if you set a member variable in didReceiveRemoteNotification, you  will know if this is from closed or background  
  NSLog(@"%@", response.notification.request.content.userInfo);

  //Adding notification here
  [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil];
}  

在iOS 10中执行didReciveRemoteNotificationNotCalled

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM