我知道这个问题在SO中已经问了很多遍了,我已经看到了所有这些。当我的应用程序位于预期的前台时,它会收到推送通知,但是当它推送到后台时,什么也没有发生。我尝试了其他问题中提到的所有方法,但均无济于事。我正在iOS 13上对此进行测试。这是我在代码中所做的。请求权限
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
switch (settings.authorizationStatus)
{
// User hasn't accepted or rejected permissions yet. This block shows the allow/deny dialog
case UNAuthorizationStatusNotDetermined:
{
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
{
if(granted)
{
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
}
}
}
}
收到通知后
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
os_log(OS_LOG_DEFAULT, "Received Notification");
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[self application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:^(UIBackgroundFetchResult result){}];
}
-(void) userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
os_log(OS_LOG_DEFAULT, "Received Notification willPresentNotification");
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center :(UNNotification *)notification{
os_log(OS_LOG_DEFAULT, "Received Notification openSettingsForNotification");
}
-(void) userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{
os_log(OS_LOG_DEFAULT, "Received Notification didReceiveNotificationResponse");
}
当应用程序处于前台状态时,一切正常,但是一旦将其推送到后台,这些日志都不会在控制台上打印。这是我到目前为止尝试过的。
我注意到的另一件事是,当应用程序被杀死并且我从通知中心点击通知时,调用了didFinishLaunchingWithOptions,而launchOptions字典为nil。
有人有过这样的经历吗?我想知道如何解决这个问题。任何帮助将不胜感激。
欢呼