当 App 从后台收到推送通知或在 iOS 中终止时,无法收听来自 NSNotificationCenter 的通知
Unable to listen notification from NSNotificationCenter when App received push notification from background or terminated in iOS
我正在 iOS 10 中实现推送通知。一切正常。
但是我需要在APP收到推送通知的时候点击API(不仅在Active状态,而且在background/terminated)。
为此,当 App 收到这样的推送通知时,我使用 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
}
当应用程序在前台 运行 时,这工作正常。
但不在后台和终止状态。
我有什么错误或我必须实施的其他事情吗?
From iOS 10, we must add UserNotifications framework and delegate
所以首先我们需要在 appDelegate.h
中做以下事情
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
前景状态
- (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 中实现推送通知。一切正常。
但是我需要在APP收到推送通知的时候点击API(不仅在Active状态,而且在background/terminated)。
为此,当 App 收到这样的推送通知时,我使用 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
}
当应用程序在前台 运行 时,这工作正常。 但不在后台和终止状态。
我有什么错误或我必须实施的其他事情吗?
From iOS 10, we must add UserNotifications framework and delegate
所以首先我们需要在 appDelegate.h
中做以下事情#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
前景状态
- (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];
}