背景:在应用没有打开时,从消息中心打开消息,在Ionic 的框架中无法调用到jPushPlugin插件的receiveNotificationIniOSCallback; (JPush用的cn.jpush.phonegap.JPushPlugin 2.0.0 "JPush Plugin"--ionic plugin add https://github.com/DongHongfei/jpush-phonegap-plugin.git )
解决方案在网上查了3天,最后总结如下:
在AppDelegate.m 中的 didFinishLaunchingWithOptions 方法中{
// 原有代码
//JPush添加代码
// [color=red]再加入[/color]
// add code
if (launchOptions) {
NSDictionary * notificationKey = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(notificationKey){
BOOL isActive;
if (application.applicationState == UIApplicationStateActive) {
isActive = TRUE;
} else {
isActive = FALSE;
}
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"",@"isActive",nil];
[userInfo addEntriesFromDictionary:notificationKey];
[userInfo setValue: [[NSNumber alloc] initWithBool:isActive] forKey:@"isActive" ];
double delayInSeconds = 3.0;//需要等待框架加载完成才能调用receiveNotificationIniOSCallback
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error];
NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *javascript = [NSString stringWithFormat:@"window.plugins.jPushPlugin.receiveNotificationIniOSCallback('%@')",jsonString ];
[viewController.webView stringByEvaluatingJavaScriptFromString:javascript];
});
}
}
}
解决方案在网上查了3天,最后总结如下:
在AppDelegate.m 中的 didFinishLaunchingWithOptions 方法中{
// 原有代码
//JPush添加代码
// [color=red]再加入[/color]
// add code
if (launchOptions) {
NSDictionary * notificationKey = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(notificationKey){
BOOL isActive;
if (application.applicationState == UIApplicationStateActive) {
isActive = TRUE;
} else {
isActive = FALSE;
}
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"",@"isActive",nil];
[userInfo addEntriesFromDictionary:notificationKey];
[userInfo setValue: [[NSNumber alloc] initWithBool:isActive] forKey:@"isActive" ];
double delayInSeconds = 3.0;//需要等待框架加载完成才能调用receiveNotificationIniOSCallback
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:&error];
NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *javascript = [NSString stringWithFormat:@"window.plugins.jPushPlugin.receiveNotificationIniOSCallback('%@')",jsonString ];
[viewController.webView stringByEvaluatingJavaScriptFromString:javascript];
});
}
}
}