Suggestion on how to link APN's device token to a registered user (through phonegap or UIWebView)

前端 未结 3 1084
南旧
南旧 2021-02-01 11:52

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 t

3条回答
  •  既然无缘
    2021-02-01 11:55

    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 
    #import 
    
    @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

提交回复
热议问题