如何在Phonegap iOS中将设备令牌从Xcode抓取到javascript? [英] How to grab device Token from Xcode to javascript in phonegap ios?

查看:98
本文介绍了如何在Phonegap iOS中将设备令牌从Xcode抓取到javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用phonegap向用户开发IOS应用,并且我正在为这些应用做推送通知,请按照此处的教程进行操作:

I am using phonegap to develope IOS apps to user,and i doing a push notification for the apps,follow the tutorial here:

http://devgirl .org/2012/10/19/tutorial-apple-push-notifications-with-phonegap-part-1/

我在注册设备令牌时遇到问题.我想将设备令牌从xcode代码存储在javascript中,但是我不知道为什么我要使用phonegap开发应用程序的任何Objective-C语言.我尝试研究如何找到deviceToken在Xcode中.然后在此处显示示例

And i have problem in register device token.I want to store the deviceToken in javascript from xcode code ,but i dont know any objective-C language that why i using phonegap to develop apps.I try research how to find the deviceToken in Xcode.Then example show here

   - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
  {
    NSLog(@"My token is: %@", deviceToken);
  }

我如何将deviceToken字符串变量转换为javascript?是否可以做到这一点?

how can i take the deviceToken string variable to javascript?Is it possible to do that?

推荐答案

我写了您正在使用的教程,并且其中有一个指向

I wrote the tutorial you are using, and in there is a link to a sample on github that actually uses a generic PushNotification plugin with PhoneGap. In the sample code you can see it using the plugin to pass back the device token so you can store it from your JavaScript.

这是您在使用插件的示例中的Objective-c中所引用的方法:

Here's the method you're referring to in objective-c from the sample that uses the plugin:

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"Calling push notification next, my registered token is: %@", deviceToken);
    // Code for phonegap communication - calls this method in PushNotification.m
    PushNotification* pushHandler = [self.viewController getCommandInstance:@"PushNotification"];
    [pushHandler didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

以下代码将调用PushNotification插件代码:

Which invokes the PushNotification plugin code in the following:

- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken:%@", 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"]]];

}

然后在JavaScript中,您可以从回调中引用它:

Then in JavaScript you can reference it from the callback:

register: function() {
    var pushNotification = window.plugins.pushNotification;
    pushNotification.registerDevice({alert:true, badge:true, sound:true}, function(status) {
        app.myLog.value+=JSON.stringify(['registerDevice status: ', status])+"\n";
        app.storeToken(status.deviceToken);
    });
},
storeToken: function(token) {
    console.log("Token is " + token);
    ...
}    

查看完整的示例代码,并

Check out the full sample code and my tutorial for more details...

希望有帮助:)

这篇关于如何在Phonegap iOS中将设备令牌从Xcode抓取到javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆