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

查看:16
本文介绍了如何在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 代码将 deviceToken 存储在 javascript 中,但我不知道为什么我使用 phonegap 开发应用程序的任何目标 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?

推荐答案

我写了你正在使用的教程,里面有一个github 上的示例,实际上使用带有 PhoneGap 的通用 PushNotification 插件.在示例代码中,您可以看到它使用插件传回设备令牌,以便您可以从 JavaScript 存储它.

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天全站免登陆