【问题标题】:Urbanairship delegate not workingUrbanairship 代表不工作
【发布时间】:2016-07-05 12:20:30
【问题描述】:

我正在使用 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);
}

但是当我收到 push 时,没有一个函数在调用。我在哪里做错了?

【问题讨论】:

  • 嗨。你找到解决办法了吗?

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


【解决方案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;
}

【讨论】:

  • 嗨..感谢您的回答,但我仍然只能访问前台通知。当应用程序在后台时,我无法获取推送,推送正在接收,但它没有触发“receivedBackgroundNotification:”方法。
  • APNS 仅在您发送内容可用推送并且启用后台远程通知时才会唤醒应用程序。还要确保启用了后台刷新,并且您没有通过任务切换器杀死应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多