iOS普通推送以及静默推送奇怪的注意事项

admin 2022-5-7 959

  1. 如果不设置普通推送delegate,则普通推送会走静默推送回调。
  2. 程序每次卸载+重装都会更新token。
  3. 即使弹窗用户点击不允许,依旧可以获取token。
  4. 设置中的关闭通知,仅对普通推送有效,对静默推送不起作用。同理,关闭后台刷新只对静默推送有效,对普通推送不起作用。
  5. 静默推送会强行启动App,不管App是Active、Suspend、Killed或者任何情况,记住是任何情况。
  6. 静默推送启动App会走application:didFinishLaunchingWithOptions:,但是不会触发UIApplicationDidBecomeActiveNotification
  7. 静默推送会自动启动App,并且最长维持30秒,用户再次点击时不会再触发application:didFinishLaunchingWithOptions:
  8. 苹果后台申请推送证书以及Identifiers时,只需要勾选Push Notifications即可实现两种推送功能。
  9. 普通推送只有在点击通知栏进入App后,才会走对应的回调。
  10. 网上的其他教程大部分在瞎扯淡。

推送代码

#import <UserNotifications/UserNotifications.h>@interface AppDelegate () <UNUserNotificationCenterDelegate>@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"启动了");
    [self setNormalPushNotification];
    return YES;}// 初始化普通推送- (void)setNormalPushNotification {
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        NSLog(@"是否允许通知: %d", granted);
    }];
    
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    
    // ⚠️注意: 如果不设置delegate,普通推送也会走didReceiveRemoteNotification
    [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];}// App在前台时收到普通推送- (void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler {
    NSDictionary* userInfo = notification.request.content.userInfo;
    NSLog(@"前台收到普通推送: %@", userInfo);}// App在后台时点击普通推送栏- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
    NSDictionary* userInfo = response.notification.request.content.userInfo;
    NSLog(@"后台收到普通推送: %@", userInfo);
    completionHandler();}// APNS注册成功,返回token- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    if (![deviceToken isKindOfClass:[NSData class]]) return;
    const unsigned *tokenBytes = [deviceToken bytes];
    NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
    ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
    ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
    ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
    NSLog(@"deviceToken:%@",hexToken);}// APNS注册失败- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Error: %@", error);}// App在任何状态下收到静默推送- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"收到静默推送: %@", userInfo);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Recieve" object:nil];
    
    UIApplicationState state = [UIApplication sharedApplication].applicationState;
     if(state == UIApplicationStateBackground) {
            NSLog(@"App在后台,运行5秒");
     }  else if (state == UIApplicationStateActive) {
            NSLog(@"App在前台");
     }

    // 5秒后通知系统将处于后台的App挂起(suspend)
    [NSTimer scheduledTimerWithTimeInterval:5 repeats:NO block:^(NSTimer * _Nonnull timer) {
        completionHandler(UIBackgroundFetchResultNewData);
    }];}@end
       

推送JSON测试用例

// 静默推送{"aps":{"content-available":1}}// 普通推送{"aps":{"alert":"普通推送","sound":"default","userinfo":{"username":"tom"}}}
       

好用的推送测试软件

https://github.com/onmyway133/PushNotifications/releases        



作者:unakayou
链接:https://www.jianshu.com/p/8b6e5bccd140
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


最新回复 (0)
返回
发新帖