如何将APNS回调放在除AppDelegate之外的其他内容中?

时间:2014-03-04 00:32:40

标签: objective-c cordova apple-push-notifications

我要求将我的PhoneGap iOS应用的用户登录名与APNS返回的deviceToken对齐。我正在使用PhoneGap来构建这个功能,所以我想写一个插件,在登录页面之后启动对APNS的需求,然后使用我从登录中获得的用户名将其保存在我们的服务器上。

到目前为止,我有那个工作但是当启动“didFinishLaunchingWithOptions”回调时它返回到AppDelegate类(PhoneGap引导程序类)并且用户的ID不在该类中,所以我有一个属性设置,以便插件类可以使用它。我的问题是,无论如何,我可以在我的插件类中保留此回调,以便它有一个userId的句柄?还有其他方法可以解决这个问题,在我看来这是一个常见的用例吗?

这是我的PhoneGap插件的(部分)代码:

    @implementation ApplePushNotificationService

    - (void)registerDevice:(CDVInvokedUrlCommand *)command {
        [self.commandDelegate runInBackground:^{
            uid = [command.arguments objectAtIndex:0];
            version = [self osVersionBuild];
            pluginResult = nil;

            // Register device for push notifications
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert |
                                                                               UIRemoteNotificationTypeBadge |
                                                                               UIRemoteNotificationTypeSound)];
            [self processToken];

            // return result
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }];
    }
    - (void) processToken
{
    // Get UID from calling class
    NSString* deviceToken = ((AppDelegate*)[[UIApplication sharedApplication] delegate]).token;

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://xxx/app/apns/register/"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    // Create JSON to POST
    NSDictionary *json = [[NSDictionary alloc] initWithObjectsAndKeys:
                          [NSString stringWithString:uid], @"uid",
                          @"ios", @"type",
                          [NSString stringWithString:deviceToken], @"registrationId",
                          @"0.0.2", @"version",
                          nil];
    NSError *error = nil;
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:&error];

    if (jsonData) {
        request.HTTPBody = jsonData;

        // Create url connection and fire request
        NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    } else {
        NSLog(@"Unable to serialize the data %@: %@", json, error);
    }
}

请注意,uid是根据传入的参数设置的,这里是回调的代码:

@synthesize token; // defined in .h as @property (nonatomic, readwrite) NSString* token;    
- (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        NSLog( @"Device token is: %@", deviceToken);
        // Convert to string that can be stored in DB
        NSString *regId = [[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""];
        regId = [regId stringByReplacingOccurrencesOfString:@">" withString:@""];
        regId = [regId stringByReplacingOccurrencesOfString: @" " withString: @""];

        self.token = regId;
    }

1 个答案:

答案 0 :(得分:0)

您无法更改此功能,因此当设备收到APNS回调时,将触发App委托中的方法来处理该问题。

您可以创建ApplePushNotificationService的单例类,并添加新方法来处理传入事件。之后,只需在您的应用代表中调用它。例如:

// AppDelegate.m

- (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [[ApplePushNotificationService sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken: deviceToken];
}

当然需要添加新方法执行ApplePushNotificationService类application: didRegisterForRemoteNotificationsWithDeviceToken:并创建新的单例sharedInstance,如下所示:

// ApplePushNotificationService.h
+ (void) application:(UIApplication *)application uploadDeviceToken:(NSString *)deviceToken;

// ApplePushNotificationService.m
+ (ApplePushNotificationService *)sharedInstance
{
    // ...
}
+ (void) application:(UIApplication *)application uploadDeviceToken:(NSString *)deviceToken
{
    //...
}

希望这个帮助