Ios 在应用程序处于后台时接收推送通知
我知道这个问题在《so》中被问了很多次,我已经看到了这一切。我的应用程序按预期在前台接收推送通知,但当推送到后台时,什么也没有发生。我尝试了其他问题中提到的一切,但都没有用。我正在iOS 13上测试这个。这就是我在代码中所做的。请求许可Ios 在应用程序处于后台时接收推送通知,ios,notifications,apple-push-notifications,ios13,Ios,Notifications,Apple Push Notifications,Ios13,我知道这个问题在《so》中被问了很多次,我已经看到了这一切。我的应用程序按预期在前台接收推送通知,但当推送到后台时,什么也没有发生。我尝试了其他问题中提到的一切,但都没有用。我正在iOS 13上测试这个。这就是我在代码中所做的。请求许可 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center getNotificationSettingsWithComplet
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");
}
当应用程序在前台时,一切正常,但一旦推到后台,这些日志就不会打印到控制台上。这就是我到目前为止所尝试的