xcode 如何在phonegap ios中将设备令牌从Xcode抓取到javascript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13757330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:15:59  来源:igfitidea点击:

How to grab device Token from Xcode to javascript in phonegap ios?

xcodecordovaapple-push-notifications

提问by user1511029

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

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

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

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

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

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

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

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

我怎样才能将 deviceToken 字符串变量带到 javascript?是否可以这样做?

回答by devgirl

I wrote the tutorial you are using, and in there is a link to a sample on githubthat 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.

我编写了您正在使用的教程,其中有一个指向github示例的链接,该示例实际上使用带有 PhoneGap 的通用 PushNotification 插件。在示例代码中,您可以看到它使用插件传回设备令牌,以便您可以从 JavaScript 存储它。

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

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

- (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];
}

Which invokes the PushNotification plugin code in the following:

其中调用 PushNotification 插件代码如下:

- (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"]]];

}

Then in JavaScript you can reference it from the callback:

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

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 tutorialfor more details...

查看完整的示例代码和我的教程以获取更多详细信息...

Hope that helps :)

希望有帮助:)

回答by Paddy

Place the following two lines of code into your didRegisterForRemoteNotificationsWithDeviceTokenof an AppDelegate.m

将以下两行代码放入您didRegisterForRemoteNotificationsWithDeviceTokenAppDelegate.m

NSString* jsString = [NSString stringWithFormat:@"var deviceToken = \"%@\";", deviceToken];
    [self.viewController.webView stringByEvaluatingJavaScriptFromString:jsString];

In JS file access the DeviceToken as

在 JS 文件中访问 DeviceToken 作为

DeviceToken = (typeof deviceToken !== "undefined") ? deviceToken : "";

And use the DeviceToken as per your requirments.

并根据您的要求使用 DeviceToken。