Suggestion on how to link APN's device token t

2019-03-09 15:18发布

问题:

Similar question here: jQueryMobile, Phonegap and Device Token - iOS

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.

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

  • What's in my mind now is to write a custom plugin and pass the device token along during user registration. Is there any better alternative?

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

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

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?

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

Any help is welcomed!

Edit: Possible solution?

Restless research emerges this possible solution:

  1. [Native] Application started - APN registration started and device_token is received
  2. [Native] Store this device_token into local store (CoreData/SqlLite or Property Lists?) and send it to server to do device_token registration
  3. [WebView] Whenever a user is login or signup, this device_token will be queried through PhoneGap, hashed and sent over to the server to do a sign in, comparison and link up.

Any unforeseen scenario is problematic?

EDIT: Answer

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

回答1:

Ok, I finally made a plugin that seems to work

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

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 - 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 - 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);
    }
};

How to use

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);
}

I made it work on PhoneGap 1.1

Hope this helps



回答2:

For the sake of completeness, this is my solution after using @TDeBailleul solution. Ported over to Cordova. (Tested on iOS only. After I have finished Android version I will post a plugin for this:

Environment

  • Cordova 1.5.0 (Formerly PhoneGap)
  • Xcode 4.3.1
  • iOS 5.1
  • Mac OS X 10.7.3
  • Do not use Automatic Reference Counting (ARC) to avoid compilation error
  • Go through this freaking long tutorial to have your Push Notification certificate ready

Working Flow

  1. [Native] Application started - APN registration started and device_token is received at server side
  2. [Native] Application store the token in AppDelegate, and to get the token, use the PushToken in the following codes
  3. [WebView] Whenever a user is login or signup, the token will be called through Cordova (formerly PhoneGap) plugin, hashed and being sent over to the server to do a sign in, matching and link up the user to a particular device.

So below is my complete working codes that do the native pulling of token. Server part is nothing but just linking accounts to devices. You should know how to do it through your favourite server side application.

AppDelegate.h

@interface AppDelegate : NSObject < UIApplicationDelegate, UIWebViewDelegate, CDVCommandDelegate > {
    ...
    NSString* token;
    ...
}
...
...
...
@property (retain, nonatomic) NSString* token;

AppDelegate.m

Note: I'm a Obj-C newbie and I have included my attempt to post token to my server as well. If you do not need the posting mechanism, retain the first 5 lines (til' self.token = dt) in the 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);
    });
});

And you should be able to get your token from your Javascript side ;)

Have fun!

Cheers



回答3:

The previous answers are really great ways to solve this problem and they do it in a very formal fashion. However, if you want a quick and dirty method of solving the problem, you can simply do this:

at the bottom of didRegisterForRemoteNotificationsWithDeviceToken add

NSString* jsString = [NSString stringWithFormat:@"var deviceToken = \"%@\";", deviceToken];
[self.viewController.webView stringByEvaluatingJavaScriptFromString:jsString];

In your javascript

deviceId = (typeof deviceToken !== "undefined") ? deviceToken : null;