Ios 关于如何链接APN和x27的建议;将设备令牌发送给注册用户(通过phonegap或UIWebView)
类似问题如下: 场景是,我有一个基于PhoneGap的web应用程序,本机iOS帮助我在APN上注册了设备,我在服务器数据库中收到了设备令牌 问题1:如何使用PhoneGap将注册用户(通过UIWebView)与此设备令牌关联Ios 关于如何链接APN和x27的建议;将设备令牌发送给注册用户(通过phonegap或UIWebView),ios,cordova,push-notification,apple-push-notifications,Ios,Cordova,Push Notification,Apple Push Notifications,类似问题如下: 场景是,我有一个基于PhoneGap的web应用程序,本机iOS帮助我在APN上注册了设备,我在服务器数据库中收到了设备令牌 问题1:如何使用PhoneGap将注册用户(通过UIWebView)与此设备令牌关联 我现在想的是编写一个自定义插件,并在用户注册期间传递设备令牌。还有更好的选择吗 问题2:由于设备令牌可以随时更改,我应该如何将此用户重新链接到此设备令牌 也许每当用户登录时,我都会执行window.plugins.PluginName.getDeviceToken并
- 我现在想的是编写一个自定义插件,并在用户注册期间传递设备令牌。还有更好的选择吗
- 也许每当用户登录时,我都会执行window.plugins.PluginName.getDeviceToken并同步它
{user\u id:123,旧设备\u令牌:“xxxx…”,新设备\u令牌:“xxx…”
我在答案中张贴了我的完整工作解决方案。请在这里查看:好的,我终于制作了一个似乎可以工作的插件 1-确保您的PhoneGap Xcode项目已针对iOS 4 SDK进行了更新 2-在插件文件夹中创建一个PushToken文件夹添加以下PushToken.m和PushToken.h文件,然后使用“为任何添加的文件夹创建组”将文件夹拖动到XCode中的插件文件夹中 3-将PushToken.js文件添加到磁盘上的www文件夹中,并将对.js文件的引用作为标记添加到html文件中 4-向PhoneGap.plist中的插件添加带有键PushToken和字符串值PushToken的新条目 PushToken.h
#import <Foundation/Foundation.h>
#import <PhoneGap/PGPlugin.h>
@interface PushToken : PGPlugin{
NSString* callbackID;
}
@property (nonatomic, copy) NSString* callbackID;
- (void) getToken:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
@interface AppDelegate : PhoneGapDelegate {
NSString* invokeString;
NSString* token;
}
@property (copy) NSString* invokeString;
@property (retain, nonatomic) NSString* token;
@interface AppDelegate : NSObject < UIApplicationDelegate, UIWebViewDelegate, CDVCommandDelegate > {
...
NSString* token;
...
}
...
...
...
@property (retain, nonatomic) NSString* token;
#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.js
var PushToken = {
getToken: function(types, success, fail) {
return PhoneGap.exec(success, fail, "PushToken", "getToken", types);
}
};
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;
如何使用
PushToken.getToken(
["getToken"] ,
function(token) {
console.log("Token : "+token);
},
function(error) {
console.log("Error : \r\n"+error);
}
);
document.addEventListener('deviceready', function() {
if (typeof PushToken == 'object') {
PushToken.install();
}
PushToken.getToken(['getToken'], function(token) {
callback(token);
}, function() {
callback(null);
});
});
AppDelegate.h
#import <Foundation/Foundation.h>
#import <PhoneGap/PGPlugin.h>
@interface PushToken : PGPlugin{
NSString* callbackID;
}
@property (nonatomic, copy) NSString* callbackID;
- (void) getToken:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
@interface AppDelegate : PhoneGapDelegate {
NSString* invokeString;
NSString* token;
}
@property (copy) NSString* invokeString;
@property (retain, nonatomic) NSString* token;
@interface AppDelegate : NSObject < UIApplicationDelegate, UIWebViewDelegate, CDVCommandDelegate > {
...
NSString* token;
...
}
...
...
...
@property (retain, nonatomic) NSString* token;
#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
AppDelegate.m
#import "PushToken.h"
#import "AppDelegate.h"
@implementation PushToken
@synthesize callbackID;
-(void)getToken:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
self.callbackID = [arguments pop];
NSString *token = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).token;
PluginResult* pluginResult = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsString:[token stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if(token.length != 0)
{
[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
}else {
[self writeJavascript: [pluginResult toErrorCallbackString:self.callbackID]];
}
}
@end
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
self.token = [[[[deviceToken description]
stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
NSLog(@"My token is: %@", self.token);
}
#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
-(void)应用程序:(UIApplication*)应用程序DIDregisterforRemotionTificationswithDeviceToken:(NSData*)deviceToken
{
self.token=[[[deviceToken描述]
StringByReplacingOfString:@“with String:@”]
StringByReplacingOfString:@“和字符串:@]”发生;
NSLog(@“我的令牌是:%@”,self.token);
}
我让它在PhoneGap 1.1上工作
希望这有帮助为了完整起见,这是我使用@tdebaileul解决方案后的解决方案。移植到Cordova。(仅在iOS上测试。完成Android版本后,我将为此发布一个插件: 环境
deviceId = (typeof deviceToken !== "undefined") ? deviceToken : null;
- Cordova 1.5.0(原PhoneGap)
- 代码4.3.1
- iOS 5.1
- Mac OS X 10.7.3
- 不要使用自动引用计数(ARC)来避免编译错误
- 请完成以准备好推送通知证书
AppDelegate
中,要获取令牌,请在以下代码中使用PushToken
#import <Foundation/Foundation.h>
#import <PhoneGap/PGPlugin.h>
@interface PushToken : PGPlugin{
NSString* callbackID;
}
@property (nonatomic, copy) NSString* callbackID;
- (void) getToken:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
@interface AppDelegate : PhoneGapDelegate {
NSString* invokeString;
NSString* token;
}
@property (copy) NSString* invokeString;
@property (retain, nonatomic) NSString* token;
@interface AppDelegate : NSObject < UIApplicationDelegate, UIWebViewDelegate, CDVCommandDelegate > {
...
NSString* token;
...
}
...
...
...
@property (retain, nonatomic) NSString* token;
#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.h
#import <Foundation/Foundation.h>
#import <PhoneGap/PGPlugin.h>
@interface PushToken : PGPlugin{
NSString* callbackID;
}
@property (nonatomic, copy) NSString* callbackID;
- (void) getToken:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
@interface AppDelegate : PhoneGapDelegate {
NSString* invokeString;
NSString* token;
}
@property (copy) NSString* invokeString;
@property (retain, nonatomic) NSString* token;
@interface AppDelegate : NSObject < UIApplicationDelegate, UIWebViewDelegate, CDVCommandDelegate > {
...
NSString* token;
...
}
...
...
...
@property (retain, nonatomic) NSString* token;
#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.js
var PushToken = {
getToken: function(types, success, fail) {
return PhoneGap.exec(success, fail, "PushToken", "getToken", types);
}
};
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;
用法
PushToken.getToken(
["getToken"] ,
function(token) {
console.log("Token : "+token);
},
function(error) {
console.log("Error : \r\n"+error);
}
);
document.addEventListener('deviceready', function() {
if (typeof PushToken == 'object') {
PushToken.install();
}
PushToken.getToken(['getToken'], function(token) {
callback(token);
}, function() {
callback(null);
});
});
您应该能够从Javascript端获取令牌;)
玩得开心
干杯前面的答案确实是解决此问题的好方法,它们以非常正式的方式进行。但是,如果您想要快速而肮脏的方法来解决此问题,您可以简单地执行以下操作: 在DID注册表底部,用于通过DeviceToken add删除通知
NSString* jsString = [NSString stringWithFormat:@"var deviceToken = \"%@\";", deviceToken];
[self.viewController.webView stringByEvaluatingJavaScriptFromString:jsString];
在您的javascript中
deviceId = (typeof deviceToken !== "undefined") ? deviceToken : null;
你在一个插件或其他可以传递给PhoneGap的东西上有什么进展吗?还没有。我还在做这个项目,一旦我得到一些东西,我会把它发到hereDid,你会设法让我的解决方案工作吗?是的,伙计,今天晚上经过一整天的探索它才工作。我已经把它转换成Cordova,现在它工作得很好。接受你的答案。我将稍后发布我的解决方案添加的答案作为我的下一篇帖子;)那么,xcode中的插件文件夹(而不是插件文件夹)在哪里?这是怎么回事?有多个
didRegisterForRemoteNotificationsWithDeviceToken
的实例。