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

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

问题描述

这里有类似的问题: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:您如何使用 PhoneGap 将注册用户(通过 UIWebView)关联到此设备令牌?

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...'}?

仅供参考,此应用程序是为事件而构建的,并且客户端已请求人们在此移动应用程序上进行消息传递.当John Doe"收到他朋友的消息时,如何向他推送新消息通知?- 问题是如何将John Doe"链接到特定的 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?

这不能太特定于 iOS,因为此应用程序也必须部署在 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. [Native] 应用程序已启动 - APN 注册已启动并收到 device_token
  2. [Native] 将此 device_token 存储到本地存储(CoreData/SqlLite 或属性列表?)并将其发送到服务器以进行 device_token 注册
  3. [WebView] 每当用户登录或注册时,此 device_token 将通过 PhoneGap 查询、散列并发送到服务器进行登录、比较和链接.

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

Any unforeseen scenario is problematic?

回答

我在答案中发布了完整的工作解决方案.在这里查看:https://stackoverflow.com/a/9628592/534862

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

推荐答案

好的,我终于做了一个看起来有效的插件

Ok, I finally made a plugin that seems to work

1 - 确保您的 PhoneGap Xcode 项目已针对 iOS 4 SDK 进行更新.

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

2 - 在您的 Plugins 文件夹中创建一个 PushToken 文件夹,将以下 PushToken.m 和 PushToken.h 文件添加到其中,然后将该文件夹拖到 XCode 中的 Plugin 文件夹中,使用为任何添加的文件夹创建组"

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 文件的引用添加为 html 文件中的标签

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 - 使用键 PushToken 和字符串值 PushToken 添加新条目到 PhoneGap.plist 中的插件

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天全站免登陆