[問題] 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