[问题] iOS notification

楼主: valda (valda)   2014-12-11 16:01:58
我在接收到notification的时候要侦测,以下几种情况
情况一:user没做任何动作,此时app在背景(UIApplicationStateBackground),
app在背景默默的更新资料
情况二:user正在使用app(UIApplicationStateActive),app在背景默默的更新
资料
情况三:used按下alert view,此时app正在背景,由背景带入前景
(UIApplicationStateInactive),app 需要push特定的view
但目前的状况是,我无法侦测出情况三,先把app缩到背景,push notification后,
按下alert view,会进去didReceiveRemoteNotification两次(是因为block的关系吗?
),第一次进去state为UIApplicationStateBackground,第二次进去刚开始是
UIApplicationStateInactive但后面又会变成UIApplicationStateBackground
有高手知道怎么处理吗?
- (void)application:(UIApplication *)application didReceiveRemoteNotification:
(NSDictionary *)userInfo fetchCompletionHandler:(void (^)
(UIBackgroundFetchResult))completionHandler{
//app 正在前景或背景执行然后使用者按下alert view时
UIApplicationState state = [[UIApplication sharedApplication]
applicationState];
if (state==UIApplicationStateActive) {
NSLog(@"UIApplicationStateActive");
}else if(state==UIApplicationStateInactive) {
NSLog(@"UIApplicationStateInactive");
}else if(state==UIApplicationStateBackground) {
NSLog(@"UIApplicationStateBackground");
}
NSLog(@"didReceiveRemoteNotification started...");
NSLog(@"userInfo -> %@",userInfo);
NSString * objectId = userInfo[@"objectId"];
PFObject *pfObject=[PFQuery getObjectOfClass:@"TestTable" objectId:objectId];
TestTable *table = [TestTable MR_findFirstByAttribute:@"objectId" withValue:objectId];
__block TestTable *tempTable=nil;
if (table==nil && pfObject!=nil) {
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
tempTable=[TestTable MR_createInContext:localContext];
tempTable.objectId=pfObject.objectId;
tempTable.subject=pfObject[@"subject"];
tempTable.content=pfObject[@"content"];
tempTable.isRead=[pfObject[@"isRead"] boolValue];
tempTable.isReceive=YES;
tempTable.updatedAt=pfObject.updatedAt;
tempTable.createdAt=pfObject.createdAt;
tempTable.receivedAt=[NSDate date];
[pfObject setObject:[NSNumber numberWithBool:YES] forKey:@"isReceive"];
[pfObject saveInBackground];
} completion:^(BOOL success, NSError *error) {
if (success) {
dispatch_async (dispatch_get_main_queue(), ^{
if (state==UIApplicationStateActive) {
NSLog(@"UIApplicationStateActive");
}else if(state==UIApplicationStateInactive) {
NSLog(@"UIApplicationStateInactive");
}else if(state==UIApplicationStateBackground) {
NSLog(@"UIApplicationStateBackground");
}
//Do something
if (state==UIApplicationStateInactive)
{
//Do checking here.
UIViewController *rootVC = self.window.rootViewController;
SWRevealViewController *swrVC = (SWRevealViewController *)rootVC;
UINavigationController *naVC = (UINavigationController *)swrVC.frontViewController;
UIViewController *currentVC = [naVC visibleViewController];
UIStoryboard *mainStoryboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
ContentVC *contentVC=[mainStoryboard instantiateViewControllerWithIdentifier:@"ContentVC"];
TestTable *tt=[TestTable MR_findFirstByAttribute:@"objectId" withValue:objectId];
contentVC.testTable=tt;
NSLog(@"testTable content-> %@",tt.content);
NSLog(@"testTable subject-> %@",tt.subject);
NSLog(@"testTable objectId-> %@",tt.objectId);
NSLog(@"contentVC testTable content:%@",contentVC.testTable.content);
if ([currentVC isKindOfClass:[ContentVC class]]) {
[naVC popToRootViewControllerAnimated:NO];
[naVC pushViewController:contentVC animated:YES];
}
if ([currentVC isKindOfClass:[EAPMessageTVC class]]) {
[naVC pushViewController:contentVC animated:YES];
}
}
if (state == UIApplicationStateActive) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"GetNotification" object:nil];
AudioServicesPlaySystemSound (1000);
}
completionHandler(UIBackgroundFetchResultNewData);
});
}
}];
}

Links booklink

Contact Us: admin [ a t ] ucptt.com