为了完整起见,这是我使用@TDeBailleul 解决方案后的解决方案。移植到科尔多瓦。 (仅在 iOS 上测试。在完成 Android 版本后,我将为此发布一个插件:
环境
- Cordova 1.5.0(以前称为 PhoneGap)
- Xcode 4.3.1
- iOS 5.1
- Mac OS X 10.7.3
- 不要使用自动引用计数 (ARC) 以避免编译错误
- 经过这个超长的教程 http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12准备好您的推送通知证书
工作流程
- [Native] 应用程序已启动 - APN 注册已启动并且在服务器端接收到 device_token
- [Native] 应用程序将令牌存储在
AppDelegate
,要获取令牌,请使用PushToken
在下面的代码中
- [WebView] 每当用户登录或注册时,令牌将通过 Cordova(以前称为 PhoneGap)插件调用、散列并发送到服务器以进行登录、将用户匹配并链接到特定设备。
下面是我完整的工作代码,用于本地拉取令牌。服务器部分只不过是将帐户链接到设备。您应该知道如何通过您最喜欢的服务器端应用程序来完成此操作。
AppDelegate.h
@interface AppDelegate : NSObject < UIApplicationDelegate, UIWebViewDelegate, CDVCommandDelegate > {
...
NSString* token;
...
}
...
...
...
@property (retain, nonatomic) NSString* token;
AppDelegate.m
注意:我是 Obj-C 新手,我也尝试将令牌发布到我的服务器。如果不需要发布机制,请保留前 5 行(直到'self.token = dt
) 在里面didRegisterForRemoteNotificationsWithDeviceToken
...
@synthesize token;
...
- (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
...
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"Did finish launching with device token %@", deviceToken);
NSString *dt = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
dt = [dt stringByReplacingOccurrencesOfString:@" " withString:@""];
self.token = dt;
NSString *dv = [[UIDevice currentDevice] systemVersion];
NSString *dn = [[UIDevice currentDevice] systemName];
NSString *nick = [[UIDevice currentDevice] name];
NSString *model = [[UIDevice currentDevice] model];
NSString *uniqueIdentifier = [[UIDevice currentDevice] uniqueIdentifier];
NSMutableString *postData = [NSMutableString stringWithString: @"&deviceToken="];
[postData appendString:dt];
[postData appendFormat:@"&uniqueIdentifier=%@&application_uuid=5ade8400-e29c-41d4-a716-3641972a2ec6", uniqueIdentifier];
[postData appendFormat:@"&source=ios&name=%@&model=%@&systemName=%@&systemVersion=%@", nick, model, dn, dv];
NSString* escapedURLString = [postData stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
@try {
NSData *postData = [escapedURLString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL: [NSURL URLWithString:@"{YOUR URL TO POST TOKEN TO SERVER}"]
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: 180];
NSString *postLength = [[NSString alloc] initWithFormat: @"%d", [postData length]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
//??
}else{
//??
}
}
@catch (NSException *exception) {
NSLog(@"Exception %@", exception);
}
}
...
PushToken.h
#import <Foundation/Foundation.h>
#import <CORDOVA/CDVPlugin.h>
@interface PushToken : CDVPlugin {
NSString* callbackID;
}
@property (nonatomic, copy) NSString* callbackID;
- (void) getToken:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
PushToken.m
#import "PushToken.h"
#import "AppDelegate.h"
@implementation PushToken
@synthesize callbackID;
- (void)getToken:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options {
NSLog(@"Did called getToken");
self.callbackID = [arguments pop];
NSString *token = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).token;
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[token stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if (token.length != 0) {
[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
}else{
[self writeJavascript: [pluginResult toErrorCallbackString:self.callbackID]];
}
}
@end
PushToken.js
var PushToken = {
/**
* Get token from the device
* @param {array} types - By default is ['getToken']
* @param {function} success Success callback, with token
* @param {function} fail Failure callback, with null
*/
getToken: function(types, success, fail) {
return Cordova.exec(success, fail, "PushToken", "getToken", types);
},
/**
* For the sake of iOS, we need an install function
*/
install: function() {
window.plugins = window.plugins || {};
window.plugins.PushToken = PushToken;
}
};
/**
* For the rest of the devices
*/
window.plugins = window.plugins || {};
window.plugins.PushToken = PushToken;
Usage
document.addEventListener('deviceready', function() {
if (typeof PushToken == 'object') {
PushToken.install();
}
PushToken.getToken(['getToken'], function(token) {
callback(token);
}, function() {
callback(null);
});
});
并且您应该能够从 Javascript 端获取令牌;)
玩得开心!
Cheers