集成個推 SDK 後 App 啟動即崩,控制台噴出兩類關鍵錯誤:缺少 remote-notification 背景模式、CALayer position contains NaN 佈局異常。本文給出雙管齊下的修復方案。
You've implemented -[application:didReceiveRemoteNotification:fetchCompletionHandler:],
but you still need to add "remote-notification" to the list of your supported UIBackgroundModes
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>
或在 Xcode → Target → Capabilities → Background Modes 勾選 Remote notifications。
*** Terminating app due to uncaught exception 'CALayerInvalidGeometry',
reason: 'CALayer position contains NaN: [nan nan]'
frame / bounds 計算出現 NaN(如除以零、未初始化的 CGFloat)。0x1661b3),設置斷點檢查對應 View 的 frame。if (CGRectIsNull(rect) || isnan(rect.origin.x)) return; 防禦性判斷。- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 1. 註冊遠程通知(iOS 10+)
if (@available(iOS 10.0, *)) {
UNUserNotificationCenter.currentNotificationCenter.delegate = self;
[UNUserNotificationCenter.currentNotificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge completionHandler:nil];
} else {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge categories:nil];
[application registerUserNotificationSettings:settings];
}
[application registerForRemoteNotifications];
// 2. 初始化個推
[GeTuiSdk startSdkWithAppId:kGtAppId delegate:self];
return YES;
}
// 兼容舊版回調(若不需要 fetchCompletionHandler 可直接刪除該方法)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[GeTuiSdk handleRemoteNotification:userInfo];
}
加上 Background Mode 並修正佈局 NaN 後,App 可正常啟動並接收推送。