首页 > 解决方案 > Remote notifications not working as expected

问题描述

I'm trying to get my remote notifications to work properly, but I've encountered some problems.

I'm sending notifications from my server with the content-available flag set to 1, such that my didReceiveRemoteNotification is being triggered, and I display the notification to the user by triggering the following method inside didReceiveRemoteNotification:

- (void) showPush:(NSString *)message
{

  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

  UNMutableNotificationContent *content = [UNMutableNotificationContent new];
  content.body = message;
  content.sound = [UNNotificationSound defaultSound];

  NSString *identifier = @"UYLLocalNotification";
  UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
                                                                        content:content trigger:nil];

  [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    if (error != nil) {
      NSLog(@"Something went wrong: %@",error);
    }
  }];
}

Notifications being sent while the app is in the suspended state are not displayed at all, but are displayed after the app has been opened and then closed again, like this:

-> App is suspended: notification is sent
-> App is opened and in  the foreground: I can see that the notification has been processed
-> App is closed and in the background: notification is being displayed with a banner

Any suggestions to why this does not work as intended would be appreciated.

标签: iosobjective-capple-push-notifications

解决方案


我认为您需要showPush从稍微不同的应用程序委托回调中调用:

- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;

此回调说明的文档:

与 application(_:didReceiveRemoteNotification:) 方法不同,该方法仅在您的应用程序在前台运行时调用,系统在您的应用程序在前台或后台运行时调用此方法。

请参阅:https ://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application?language=objc


推荐阅读