我能够通过一些漂亮的检查自己修复它......
本质上,这整件事的关键是
-(void)applicationDidEnterBackground:(UIApplication *)application;
进入快速应用切换(或控制中心)时不会调用此方法,因此您需要根据它设置检查。
@property BOOL isInBackground;
@property (nonatomic, retain) NSMutableArray *queuedNotifications;
当您收到通知时...
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState appState = application.applicationState;
// Check if we're in this special state. If so, queue the message up
if (appState == UIApplicationStateInactive && !self.isInBackground) {
// This is a special case in which we're in fast app switching or control center
if (!self.queuedNotifications) {
self.queuedNotifications = [NSMutableArray array];
}
// Queue this to show when we come back
[self.queuedNotifications addObject:userInfo];
}
}
然后当我们回来...
- (void)applicationDidBecomeActive:(UIApplication *)application {
application.applicationIconBadgeNumber = 0;
if (!self.isInBackground) {
// Show your notifications here
// Then make sure to reset your array of queued notifications
self.queuedNotifications = [NSMutableArray array];
}
}
您可能想要做的另一件事是检查这种特殊情况,即快速切换应用程序和用户去其他地方。我在设置 isInBackground BOOL 之前执行此操作。我选择将它们作为本地通知发送
-(void)applicationDidEnterBackground:(UIApplication *)application {
for (NSDictionary *eachNotification in self.queuedNotifications) {
UILocalNotification *notification = [self convertUserInfoToLocalNotification:eachNotification];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
self.queuedNotifications = [NSMutableArray array];
self.isInBackground = YES;
}