[英]Unable to listen notification from NSNotificationCenter when App received push notification from background or terminated in iOS
I'm implementing push notifications in iOS 10. Everything working well. 我正在iOS 10中实现推送通知。一切正常。
But I need to hit API when APP received push notification (not only in Active state but also in background/terminated). 但是,当APP收到推送通知时(不仅处于活动状态,而且处于后台/终止状态),我都需要点击API。
For this I'm using NSNotificationCenter
for listening notification when App received push notification like this : 为此,我使用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];
}
And I'm listening this notification in ViewController.m
like this 我像这样在ViewController.m
监听此通知
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable:) name:@"reloadTheTable" object:nil];
}
- (void)reloadTable:(NSNotification *)notification
{
// Calling API here
}
This is working fine when the app is running in the foreground. 当应用程序在前台运行时,这工作正常。 But not in background and terminate states. 但不在背景和终止状态。
Is there any mistake by me or anything else I have to implement? 我是否有任何错误或需要实施的其他任何措施?
From iOS 10, we must add UserNotifications framework and delegate 从iOS 10开始,我们必须添加UserNotifications框架并委托
So first we need to do below things in appDelegate.h 所以首先我们需要在appDelegate.h中做以下事情
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
For FOREGROUND state 对于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);
}
This is for BACKGROUND state . 这是背景状态 。
So here you need to add the notification 所以在这里您需要添加通知
- (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];
}
didReciveRemoteNotificationNotCalled in iOS 10 在iOS 10中执行didReciveRemoteNotificationNotCalled
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.