【发布时间】:2025-12-07 20:00:01
【问题描述】:
我正在我的 iOS 应用程序中实现 APNS。我在前台收到苹果推送通知,但是当我的应用进入后台或非活动模式时,我的应用没有收到任何推送通知。
我尝试的代码如下:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
NSLog(@"active");
NSDictionary *apsInfo = [userInfo valueForKey:@"aps"];
NSString *fv=[[apsInfo valueForKey:@"alert"] componentsJoinedByString:@""];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Active" message:fv delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}else if (state == UIApplicationStateBackground){
NSLog(@"background");
NSDictionary *apsInfo = [userInfo valueForKey:@"aps"];
NSString *fv=[[apsInfo valueForKey:@"alert"] componentsJoinedByString:@""];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Background" message:fv delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}else{
NSLog(@"Inactive");
NSDictionary *apsInfo = [userInfo valueForKey:@"aps"];
NSString *fv=[[apsInfo valueForKey:@"alert"] componentsJoinedByString:@""];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Inactive" message:fv delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
请告诉我哪里出错或遗漏了什么。
【问题讨论】:
标签: ios6 push-notification apple-push-notifications