【问题标题】:Urban Airship: Generate channel ID during the first run of the iOS appUrban Airship:在 iOS 应用程序首次运行期间生成频道 ID
【发布时间】:2015-10-15 08:49:23
【问题描述】:

我已经注册了Urban Airship,获得了app key、secret和master secret。我也集成了他们的库。

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

我在我的代码中调用它:

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

在应用程序的第一次运行期间,channelID 始终为空。

我能够在第二次运行期间接收到 channelID,但在第一次运行期间无法接收。无论如何都可以建议应用程序在第一次运行期间获取 channelID 的方法。提前致谢。

已编辑:

根据 ralepinski 的建议,我正在添加 '[UAirship push].registrationDelegate = self.registrationDelegate' 行代码。

在 ViewController.h 中:

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

在 ViewController.m 中我也在使用这些代码行:

-(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];
}

我在应用目标的功能部分启用了远程通知。在应用程序的第一次运行期间,我仍然将 ChannelID 设为 null。让我知道。谢谢。

【问题讨论】:

    标签: ios urbanairship.com devicetoken


    【解决方案1】:

    频道创建依赖于网络请求。它不会立即可用,但您可以使用UARegistrationDelegate 收听它何时可用。您可以在 UAPush 实例here 上分配委托:

    [UAirship push].registrationDelegate = self.registrationDelegate;
    

    有时,如果 SDK 无法生成 deviceToken,频道注册会延迟到下一个应用前台。如果您在应用程序的功能部分启用远程通知,则会立即生成设备令牌,并在第一次运行期间创建通道。

    【讨论】:

    • 嗨 ralepinski,我在应用目标的功能部分启用了远程通知。然而,在第一次运行期间并未创建频道 ID。可能是什么问题?
    • 现在可以使用了。 'setupUrbanAirship' 方法在很久以后才被调用,因此出现了错误。通过将其放在 application-did-finish-launching 委托方法中并添加您的代码,几乎立即分配了频道 ID。
    • 很高兴你知道了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多