我编写了您正在使用的教程,其中有一个指向github上的示例的链接,该示例实际上使用了带有 PhoneGap 的通用 PushNotification 插件。在示例代码中,您可以看到它使用插件传回设备令牌,以便您可以从 JavaScript 中存储它。
这是您在使用插件的示例中在 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];
}
它在以下调用 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"]]];
}
然后在 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);
...
}
查看完整的示例代码和我的教程以获取更多详细信息...
希望有帮助:)