Urbanairship代表不工作

时间:2016-07-05 12:20:31

标签: ios objective-c push-notification apple-push-notifications urbanairship.com

我使用UrbanAirship在我的iOS应用中处理推送通知。我想实现UAPushNotificationDelegate。这是我在appdelegate中的代码。

//Appdelegate.m
UAConfig *config = [UAConfig defaultConfig];
[UAirship takeOff:config];

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

[UAirship push].autobadgeEnabled = YES;
[UAirship push].userPushNotificationsEnabled = YES;
PushHandler *pushHandlerDelegate = [[PushHandler alloc] init];
[UAirship push].pushNotificationDelegate = pushHandlerDelegate;

这是我的PushHandler.h文件

// PushHandler.h
@interface PushHandler : NSObject<UAPushNotificationDelegate>

@end

然后我在我的实现文件中实现了UrbanAirship方法

- (void)receivedForegroundNotification:(NSDictionary *)notification fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Call the completion handler
   completionHandler(UIBackgroundFetchResultNoData);
}
- (void)launchedFromNotification:(NSDictionary *)notification    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
// Call the completion handler
   completionHandler(UIBackgroundFetchResultNoData);
}

- (void)launchedFromNotification:(NSDictionary *)notification actionIdentifier:(NSString *)identifier completionHandler:(void (^)())completionHandler {
// Call the completion handler
   completionHandler()
}

- (void)receivedBackgroundNotification:(NSDictionary *)notification actionIdentifier:(NSString *)identifier completionHandler:(void (^)())completionHandler {
// Call the completion handler
   completionHandler()
}

- (void)receivedBackgroundNotification:(NSDictionary *)notification fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Call the completion handler
   completionHandler(UIBackgroundFetchResultNoData);
}

但是当我收到推送时,没有这些功能正在调用。我哪里做错了?

1 个答案:

答案 0 :(得分:1)

UAPush仅存储对委托的弱引用。您需要在代码中的某处存储强引用。

示例:

@interface AppDelegate ()
@property(nonatomic, strong) PushHandler *pushHandlerDelegate;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ...

    self.pushHandlerDelegate = [[PushHandler alloc] init];
    [UAirship push].pushNotificationDelegate = self.pushHandlerDelegate;
}