wangdazhu0 2015-08-27
背景:在应用没有打开时,从消息中心打开消息,在Ionic的框架中无法调用到jPushPlugin插件的receiveNotificationIniOSCallback;(JPush用的cn.jpush.phonegap.JPushPlugin2.0.0"JPushPlugin"--ionicpluginaddhttps://github.com/DongHongfei/jpush-phonegap-plugin.git)
解决方案在网上查了3天,最后总结如下:
在AppDelegate.m中的didFinishLaunchingWithOptions方法中{
//原有代码
//JPush添加代码
//再加入
//addcode
if(launchOptions){
NSDictionary*notificationKey=[launchOptionsobjectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(notificationKey){
BOOLisActive;
if(application.applicationState==UIApplicationStateActive){
isActive=TRUE;
}else{
isActive=FALSE;
}
NSMutableDictionary*userInfo=[NSMutableDictionarydictionaryWithObjectsAndKeys:@"",@"isActive",nil];
[userInfoaddEntriesFromDictionary:notificationKey];
[userInfosetValue:[[NSNumberalloc]initWithBool:isActive]forKey:@"isActive"];
doubledelayInSeconds=3.0;//需要等待框架加载完成才能调用receiveNotificationIniOSCallback
dispatch_time_tpopTime=dispatch_time(DISPATCH_TIME_NOW,(int64_t)(delayInSeconds*NSEC_PER_SEC));
dispatch_after(popTime,dispatch_get_main_queue(),^(void){
NSError*error;
NSData*jsonData=[NSJSONSerializationdataWithJSONObject:userInfooptions:0error:&error];
NSString*jsonString=[[NSStringalloc]initWithData:jsonDataencoding:NSUTF8StringEncoding];
NSString*javascript=[NSStringstringWithFormat:@"window.plugins.jPushPlugin.receiveNotificationIniOSCallback('%@')",jsonString];
[viewController.webViewstringByEvaluatingJavaScriptFromString:javascript];
});
}
}
}