iosurbanairship.comdevicetoken

Urban Airship: Generate channel ID during the first run of the iOS app


I have registered with Urban Airship, obtained the app key, secret and master secret. I have integrated their library as well.

-(void)setupUrbanAirship
{
    UAConfig *config = [UAConfig defaultConfig];
    config.detectProvisioningMode = YES;
    config.productionLogLevel=NO;
    config.inProduction = NO;
    config.developmentAppKey = @"XXXXXXXXXXXXXXXXXXX";
    config.developmentAppSecret = @"XXXXXXXXXXXXXXXXX";

    [UAirship takeOff:config];

    [UAirship push].userPushNotificationsEnabled = YES;

    [UAirship push].userNotificationTypes = (UIUserNotificationTypeAlert |
                                             UIUserNotificationTypeBadge |
                                             UIUserNotificationTypeSound);
}

I am calling this in my code:

NSString *channelID = [UAirship push].channelID;
NSLog(@"channelID %@",channelID);

During the first run of the app the channelID is always null.

I am able to receive the channelID during the second run but not during the first run. Could anyway suggest a way for the app to obtain the channelID during the first run itself. Thanks in advance.

Edited:

According to ralepinski's suggestion I am adding the '[UAirship push].registrationDelegate = self.registrationDelegate' line of code.

In ViewController.h :

@interface ViewController : UIViewController
{
    IBOutlet UIButton *selectButton;
    NSTimer *channelIDDetectionTimer;
}
@property (nonatomic, weak, nullable) id<UARegistrationDelegate> registrationDelegate;

In ViewController.m I am also using these lines of code:

-(void)initRegistration
{
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];

    NSString *channelID = [UAirship push].channelID;
    NSLog(@"channelID %@",channelID);
    if (channelID!=nil)
    {
        [delegate saveChannelID];
        if (channelIDDetectionTimer!=nil)
        {
            [channelIDDetectionTimer invalidate];
            channelIDDetectionTimer = nil;
        }
    }
    else
    {
        [UAirship push].registrationDelegate = self.registrationDelegate;
        //[[UAPush alloc]updateRegistration];
            // Registering for only UIRemoteNotificationTypeNone will not result in a
            // device token registration call. Instead update chanel registration directly.
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    channelIDDetectionTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(initRegistration) userInfo:nil repeats:YES];
}

I have enabled remote-notifications in the capabilities section of the app target. Still I get the ChannelID as null during the app's first run. Let me know. Thanks.


Solution

  • The channel creation relies on a network request. It wont immediately be available, but you can listen for when its available using UARegistrationDelegate. You can assign the delegate on the UAPush instance here:

    [UAirship push].registrationDelegate = self.registrationDelegate;
    

    Sometimes the channel registration is delayed to the next app foreground if the SDK is unable to generate a deviceToken. If you enable remote-notifications in the capabilities section of your application the device token will be generated immediately, and the channel created during the first run.