iOS 推送问题全解答《十万个为啥吖》

1,434 阅读4分钟

Q 1:为啥收不到推送(1)?

如果收到推送时,App 在前台运行,那么:

  • iOS 10 before 顶部横幅不会弹出。没有任何展示,你以为「没有收到推送」。
  • iOS 10 after 如果没有实现以下代码,也是不会有任何提示的,你以为「没有收到推送」。
    //UNUserNotificationCenterDelegate
    -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
      completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
    }

Q 2:为啥收不到推送(2)?

iOS 使用推送需要 配置推送证书,如果你配过了,仍然收不到推送,建议再配一遍。笔者曾经多次远程控制手把手给其他开发者配置证书,结果全都重新配一遍就能收到推送了。

Q 3:为啥收不到推送(3)?

Xcode 8 这里打开。

Q 4:为啥收不到推送(4)?

推送时开发环境、发布环境一一对应。环境不对应,收不到。

Q 5:为啥收不到推送(5)?

要在 Apple Developer Center 把你的测试设备加入到 Device 里面。

Q 6:为啥收不到推送(6)?

一台手机能收到,另一台不能收到。要把你的另一台测试设备也加入到 Apple Deveice 里面。。。

Q 7:为啥收不到推送(7)?

高峰时段你再等几秒就收到了。

Q 8:为啥还是收不到推送(8)???

Apple 服务器宕机了,并不是所有的设备都收不到推送了,而是新的设备无法成功注册推送服务了。

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
}

在该方法中,无法返回有效的 deviceToken 值了。等两天就好了。

Q 9:为啥生产环境收不到推送?

打包,安装,测试。即可。

Q 10:为啥 App 右上角的角标 badge 不清 0?

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

Q 11:为啥 App 右上角的角标 badge 不自增?

推送时 badge 参数设置: +n自增、-n自减、n固定值。
自增、自减一般第三方推送服务支持才有。

Q 12:为啥 App 在后台收到推送代码不执行?

Background Remote Notification

Q 13:为啥 App 在杀死后收到推送代码不执行?

App 杀死啥代码都不能执行。iOS 10 你可以试试 Notification Service Extension,详情见 这篇文章 的 Service Extension 部分。

Q 14:为啥收到推送没声音(1)?

哥哥手机左侧静音键麻烦拨上来。

Q 15:为啥收到推送没声音(2)?

推送时 sound 字段设置为:default

Q 16:为啥收到推送不播放自定义声音(1)?

1.声音文件拖拽并拷贝并生成索引到工程任意位置(在 Xcode 里拖拽)
2.推送时 sound 字段设置为:name.mp3。需要名字后缀完全一样。

Q 17:为啥收到推送不播放自定义声音(2)?

拖拽文件时没有选择拷贝,连线测试有声音,拔线没声音,声音文件在电脑里,拔线后缺失。从工程目录删除该文件,再重新添加再 build。

Q 18:为啥点击 App 图标进入 App 收不到推送(1)?

要点推送横幅、通知中心条目进入 App 才能收到。

Q 19:为啥点击 App 图标进入 App 收不到推送(2)?

解决方法:再发一条透传消息。只处理消息,不处理推送。不论点哪里进的 App,都能保证该事件被处理。

Q 20:为啥推送一条手机会收到两条一样的?

iOS 9 的 bug。

Q 21:为啥点击横幅不能跳转到指定的界面?

  • 点击推送启动 App 在这里捕获推送内容:
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
        NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
    }
  • 点击推送从后台唤醒 App 在这里捕获推送内容:
    • iOS 7 before
      - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
      }
    • iOS 7~9
      - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
      }
    • iOS 10
      //UNUserNotificationCenterDelegate
      - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
         NSDictionary *userInfo = response.notification.request.content.userInfo;
      }

Q 22:为啥点击了推送,通知中心条目不消除(1)?

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

Q 23:为啥点击了推送,通知中心条目不消除(2)?

推送时 badge 参数为 0,再调用 Q 22 导致通知中心没有发生变化,此时应设为1,再设为0:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

Q 24:为啥用户不允许通知,以后再也不能弹出请求通知权限的提示窗了?

先获取用户推送权限设置情况:

//  iOS 8 before
  UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

//  iOS 8~9
  UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
  UIUserNotificationType type = settings.types;

//  iOS 10
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  }

如果没开,跳转到 设置 - yourApp 页面让用户设置:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

Q 25:为啥 iOS 10 不能注册推送服务?

//  iOS 8 before
UIRemoteNotificationType type = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:type];

//  iOS 8~9
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:type categories:nil];
[application registerUserNotificationSettings:setting];

//  iOS 10
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (!error) {
      NSLog(@"request authorization succeeded!");
    }
}];

Q 26:为啥楼主懂的这么多?

因为他是极光推送的可耐程序猿葛格^^。