如何APN的设备令牌链接到注册用户的建议(通过PhoneGap的或UIWebView的) [英] Suggestion on how to link APN's device token to a registered user (through phonegap or UIWebView)

查看:115
本文介绍了如何APN的设备令牌链接到注册用户的建议(通过PhoneGap的或UIWebView的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似的问题在这里:<一href=\"http://stackoverflow.com/questions/9022983/jquerymobile-phonegap-and-device-token-ios\">jQueryMobile, PhoneGap的和设备令牌 - iOS设备

的情况是,我有这个PhoneGap的基于Web的应用程序,而iOS的原生帮我注册的APN设备和我在服务器数据库接收设备令牌。

The scenario is, I have this PhoneGap web based application, and the native iOS help me registered the device on APN and I received the device token in my server database.

问题1:如何注册用户(通过一个UIWebView)此设备令牌才能通过PhoneGap的关联

Question 1: How do you associate a registered user (through the UIWebView) to this device token using PhoneGap?


  • 现在什么在我的心中是编写自定义插件,用户在注册过程中令牌传递下去设备。有没有更好的选择?

问题2:由于device_token可定时改变的时候,我应该怎么重新链接该用户对这个device_token

Question 2: Since device_token can be changed from time to time, how should I re-link this user to this device_token?


  • 也许只要用户登录,我做了window.plugins。 PluginName 的.getDeviceToken和同步呢?

  • {USER_ID:123,old_device_token:XXXX ..',new_device_token:'XXX ...'}

  • Perhaps whenever the user login, I do a window.plugins.PluginName.getDeviceToken and sync it?
  • {user_id:123, old_device_token: 'xxxx..', new_device_token: 'xxx...'}?

据透露,这个应用程序是专为一个事件,客户端已要求人民对人民的短信在这个移动应用程序。你怎么推的新信息的通知的为李四的时候,他接到了朋友的消息? - 问题是如何李四链接到一个特定device_token

Fyi, this application is built for an event and the client has requested people to people messaging on this mobile app. How do you push a new message notification to "John Doe" when he received a message from his friend? - Question is how to link "John Doe" to a specific device_token?

本次申请已被部署在Android以及(C2DM)。

This could not be too iOS specific as this application has to be deployed on Android as well (C2DM).

任何帮助,欢迎!

编辑:可能的解决方法

不宁研究产生这种可能的解决方案:

Restless research emerges this possible solution:


  1. [本地]申请开始 - APN注册开始,device_token收到

  2. [本地]存储此device_token到本地存储(CoreData / SqlLite或属性列表?),并将其发送给服务器做device_token注册

  3. [网页视图]每当用户登录或注册,这device_token将通过PhoneGap的查询,散列并将它们发送至服务器做,比较标志和连接起来。

任何不可预见的情况是有问题的?

Any unforeseen scenario is problematic?

编辑:答案

我有完整的工作解决方案张贴在答案。看看这里: http://stackoverflow.com/a/9628592/534862

I have my complete working solution posted in the answer. Check it out here: http://stackoverflow.com/a/9628592/534862

推荐答案

好吧,我终于,似乎工作插件

Ok, I finally made a plugin that seems to work

1 - 确保您的PhoneGap X code ++项目已为iOS 4的SDK进行了更新

1 - Make sure your PhoneGap Xcode project has been updated for the iOS 4 SDK.

2 - 你的插件文件夹中创建一个文件夹PushToken以下PushToken.m和PushToken.h文件添加到它,然后将文件夹拖到X code中的插件文件夹,使用创建任何添加的文件夹组

2 - Create a PushToken folder in your Plugins folder add the following PushToken.m and PushToken.h files to it and then drag the folder to the Plugin folder in XCode, using "Create groups for any added folders"

3 - 将PushToken.js文件添加到磁盘上的www文件夹,并以.js文件添加引用(S)在你的HTML文件(S)标记

3 - Add the PushToken.js files to your www folder on disk, and add reference(s) to the .js files as tags in your html file(s)

4 - 添加新条目与PhoneGap.plist PushToken到插件关键PushToken和字符串值

4 - Add new entry with key PushToken and string value PushToken to Plugins in PhoneGap.plist

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

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

PushToken.js

var PushToken = {
    getToken: function(types, success, fail) {
        return PhoneGap.exec(success, fail, "PushToken", "getToken", types);
    }
};

如何使用

PushToken.getToken(     
    ["getToken"] ,           
    function(token) {
        console.log("Token : "+token); 
    },
    function(error) {
        console.log("Error : \r\n"+error);      
    }
);

AppDelegate.h

@interface AppDelegate : PhoneGapDelegate {
    NSString* invokeString;
    NSString* token;
}

@property (copy)  NSString* invokeString;
@property (retain, nonatomic) NSString* token;

AppDelegate.m

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    self.token = [[[[deviceToken description]
                         stringByReplacingOccurrencesOfString: @"<" withString: @""]
                        stringByReplacingOccurrencesOfString: @">" withString: @""]
                       stringByReplacingOccurrencesOfString: @" " withString: @""];

    NSLog(@"My token is: %@", self.token);
}

我做了它的PhoneGap 1.1工作

I made it work on PhoneGap 1.1

希望这有助于

这篇关于如何APN的设备令牌链接到注册用户的建议(通过PhoneGap的或UIWebView的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆