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

依然范特西╮ 提交于 2020-01-03 05:56:51

问题


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/

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);
  }

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


回答1:


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.

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

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

}

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...

Hope that helps :)




回答2:


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

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

In JS file access the DeviceToken as

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

And use the DeviceToken as per your requirments.



来源:https://stackoverflow.com/questions/13757330/how-to-grab-device-token-from-xcode-to-javascript-in-phonegap-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!