IOS收到推送消息后,不调用APNs通知回调方法

dailinyi
7年前 2.4k 0

app相关代码

AppDelegate.m

@interface AppDelegate ()<JPUSHRegisterDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  ...
  //初始化APNS
  JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
  if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
    // 可以添加自定义categories
    // NSSet<UNNotificationCategory *> *categories for iOS10 or later
    // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
  }
  [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
  //极光推送
  [JPUSHService setupWithOption:launchOptions appKey:@"xxxxxxxx"
                        channel:@"AppStore"
               apsForProduction:0 //1代表生产环境  0代表开发环境
          advertisingIdentifier:nil];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  /// Required - 注册 DeviceToken
  [JPUSHService registerDeviceToken:deviceToken];

//  [[NIMSDK sharedSDK] updateApnsToken:deviceToken];

}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
  NSLog(@"fail to get apns token :%@",error);
}

#pragma mark- JPUSHRegisterDelegate

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  // Required
  NSDictionary * userInfo = notification.request.content.userInfo;
  NSLog(@"------------------444444");
  if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {


    [[UserRecordUtils shared] savePushInfoWithInfo:userInfo.yy_modelToJSONString];
    [JPUSHService handleRemoteNotification:userInfo];
  }
  completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
  // Required
  NSDictionary * userInfo = response.notification.request.content.userInfo;
  NSLog(@"------------------333333");
  if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [JPUSHService handleRemoteNotification:userInfo];
  }
  completionHandler();  // 系统要求执行这个方法
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  NSLog(@"------------------111111");
  // Required, iOS 7 Support
  [JPUSHService handleRemoteNotification:userInfo];
  completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
  NSLog(@"------------------222222");
  // Required,For systems with less than or equal to iOS6
  [JPUSHService handleRemoteNotification:userInfo];
}

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate> 

@property (nonatomic, strong) UIWindow *window;

@end

出现问题

  • 这段代码初始化后,能收到极光的推送消息
  • 但是无论是收到消息,还是通过消息点击,都不会再后台打印预期的:----------------xxxxxx

操作步骤

  • 直接在xcode选择真机 -> RUN -> (iphone运行后) -> 按Home键让app进入后台 -> 到极光官网进行推送 -> 推送成功收到消息 -> 点击消息,在xcode控制台收不到打印日志

1个回答

热门排序
  • 点击消息走获取消息内容: 点击没走代码方法,那走了什么呢?日志里面有什么信息呢? 展示全部