简体   繁体   English

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

[英]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.

相关问题 如果应用终止,则未收到iOS FCM推送通知 - iOS FCM push notification not received if app is terminated 当应用程序在iOS 10中处于后台时,未收到推送通知 - Push notification not received when App is in Background in iOS 10 当应用程序在后台时,来自 iOS 推送通知操作的 Http 请求 - Http request from iOS push notification action when app is in background 收到NSNotificationCenter的通知时如何更改帧矩形? - How to change frame rect when received a notification from NSNotificationCenter? 应用程序终止时推送通知 - Push notification when app is terminated 当应用程序在iOS 10的后台运行时无法访问推送通知 - Unable to access push notification when app is in background for iOS 10 应用从后台终止时如何获取来电通知 - How to get incoming call notification when app is terminated from background 收到来自 Firebase 的推送通知 ios 但没有声音或在通知中心 - Push notification ios from Firebase is received but no sound or in notification center 当应用程序在 ios 13 中的静默推送通知中终止时运行代码 - Run code when app is terminated on silent push notification in ios 13 FCM iOS 推送通知在应用程序终止时捕获自定义数据? - FCM iOS push notification capture custom data when app is terminated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM