Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在jquerymobile中将数据从appdelegate传递到index.html_Javascript_Jquery - Fatal编程技术网

Javascript 如何在jquerymobile中将数据从appdelegate传递到index.html

Javascript 如何在jquerymobile中将数据从appdelegate传递到index.html,javascript,jquery,Javascript,Jquery,这是appdelegate.m,用于获取设备令牌,与如何在index.js中的phonegap中写入相同 Appdelegate.m (BOOL) application: (UIApplication * ) application didFinishLaunchingWithOptions: (NSDictionary * ) launchOptions { CGRect screenBounds = [ [UIScreen mainScreen] bounds

这是appdelegate.m,用于获取设备令牌,与如何在index.js中的phonegap中写入相同

Appdelegate.m

(BOOL) application: (UIApplication * ) application didFinishLaunchingWithOptions: (NSDictionary * ) launchOptions {
    CGRect screenBounds = [
        [UIScreen mainScreen] bounds
    ];
    [
        [UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)
    ];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0; - (void) application: (UIApplication * ) application didReceiveRemoteNotification: (NSDictionary * ) userInfo {} - (void) application: (UIApplication * ) application didRegisterForRemoteNotificationsWithDeviceToken: (NSData * ) deviceToken {
        NSString * devtoken = [NSString stringWithFormat: @"%@", deviceToken];
        NSLog(@"the devicetoken is %@", devtoken);
        NSRange r1 = [devtoken rangeOfString: @"<"];
        NSRange r2 = [devtoken rangeOfString: @">"];
        NSRange rSub = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length);
        NSString * sub = [devtoken substringWithRange: rSub];
        NSLog(@"%@", sub);
        NSString * newString = [sub stringByReplacingOccurrencesOfString: @" "
            withString: @""
        ];
        UIAlertView * message = [
            [UIAlertView alloc] initWithTitle: @"Device Token!"
            message: newString
            delegate: nil
            cancelButtonTitle: @"OK"
            otherButtonTitles: nil
        ];
        [message show];
        NSLog(@"%@", newString);
    } - (void) application: (UIApplication * ) application didFailToRegisterForRemoteNotificationsWithError: (NSError * ) error {
        NSLog(@"the reason for fail to register the remote notifications is %@", error);
    }
(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
CGRect屏幕边界=[
[UIScreen mainScreen]边界
];
[
[UIApplication sharedApplication]RegisterForRemotionTificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)
];
[UIApplication sharedApplication].applicationIconBadgeNumber=0;-(无效)应用程序:(UIApplication*)应用程序DidReceiveMemotentification:(NSDictionary*)用户信息{}-(无效)应用程序:(UIApplication*)应用程序DidRegisterForMemotentificationswithDeviceToken:(NSData*)设备Token{
NSString*devtoken=[NSString stringWithFormat:@“%@”,deviceToken];
NSLog(@“devicetoken为%@”,devtoken);
NSRange r1=[devtoken rangeOfString:@”“];
NSRange rSub=NSMakeRange(r1.location+r1.length,r2.location-r1.location-r1.length);
NSString*sub=[devtoken substringWithRange:rSub];
NSLog(@“%@”,sub);
NSString*新闻字符串=[子字符串ByReplacingOccurrencesofString:@“”
带字符串:@“”
];
UIAlertView*消息=[
[UIAlertView alloc]initWithTitle:@“设备令牌!”
消息:新闻字符串
代表:无
取消按钮:@“确定”
其他按钮:无
];
[信息显示];
NSLog(@“%@”,新闻字符串);
}-(无效)应用程序:(UIApplication*)应用程序未能注册远程通知,错误为:(N错误*)错误{
NSLog(@“无法注册远程通知的原因是%@”,错误);
}

您可以在js文件中获取设备令牌,如

- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    DLog(@"didRegisterForRemoteNotificationsWithDeviceToken:%@", deviceToken);

    NSString *token = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""]
                    stringByReplacingOccurrencesOfString:@">" withString:@""]
                   stringByReplacingOccurrencesOfString: @" " withString: @""];

    NSMutableDictionary *results = [PushNotification getRemoteNotificationStatus];
    [results setValue:token forKey:@"deviceToken"];

    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:results];
    [self writeJavascript:[pluginResult toSuccessCallbackString:[self.callbackIds valueForKey:@"registerDevice"]]];
}
-(void)用于删除与deviceToken(NSData*)deviceToken相关的通知的DID寄存器{
DLog(@“DidRegisterforRemotionTificationswithDeviceToken:%@”,deviceToken);
NSString*标记=[[[deviceToken description]stringByReplacingOccurrencesOfString:@”“with String:@”“]
StringByReplacingOfString:@“和字符串:@]”发生;
NSMutableDictionary*结果=[PushNotification getRemoteNotificationStatus];
[结果设置值:令牌forKey:@“deviceToken”];
CDVPluginResult*pluginResult=[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:results];
[self-writeJavascript:[pluginResult to SuccessCallBackstring:[self.callbackIds valueForKey:@“registerDevice”]];
}
在上面的代码中,pluginResult将被传递,像wise一样,我们可以在回调函数中获得deviceToken

请从下面的链接找到推送通知插件


你检查了github上的推送通知插件。我检查了它是否在appdelegate中出现。m最后设备令牌出现了,但它必须存储在html变量中,即index.html请使用PluginResult按照我下面的答案操作。你可以使用“self-writeJavascript”将deviceToken传递到html在这段代码中。在appdelegate本身中。上述方法在您给定的代码中也可用。请检查它。您是否为phonegap实现了推送通知插件?再次检查我的答案。我也在获取设备令牌,但我必须将此令牌发送到写在index.htmld中的服务器url。您知道CDVPluginResult吗?