我使用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);
}
但是当我收到推送时,没有这些功能正在调用。我哪里做错了?
答案 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;
}