问题
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