isRegisteredForRemoteNotifications locking UI with

2019-07-31 14:27发布

问题:

There's a strange situation happening in my iOS application when it receive push notification. The UI stay locked and nothing works. When I pause the debugger I see semaphore_wait_trap in my thread.

Debbuging the code I can see it is related to two things:

  • the value type in push notification (because when I change Number to String the problem disappear);
  • the isRegisteredForRemoteNotifications method (because when I remove it the problem disappear);

I'm receiving a push notification as follow

{aps: 
    {alert: { loc-args: [Fiat, Bravo, 501],
     loc-key: SOME_TEXT 
    },
    badge: 0,
    sound: default.aiff
    }
}

I made a new and simple project in Xcode to prove what I'm saying. I'm using the previous bundle identifier to receive the same push. Follow the code in AppDelegate that shows the problem:

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}

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

    NSLog(@"My token is: %@", deviceToken);
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
    //    [DefaultMethods saveInUserDefaults:@(1) forKey:kUserWasAskedForNotificationKey];
    NSLog(@"Failed to get token, error: %@", error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    if( [[UIApplication sharedApplication] isRegisteredForRemoteNotifications] ){
        NSLog(@"Success");
    }
}


@end

Thank you for any help!

回答1:

I was dealing with this problem too and have found this error in my device's logs:

com.apple.usernotifications.usernotificationservice: Exception caught during decoding of received message, dropping incoming message. Exception: Exception while decoding argument 0 (#2 of invocation): Exception: value for key 'NS.objects' was of unexpected class 'NSNumber'. Allowed classes are '{( NSString, NSArray )}'.

After calling isRegisteredForRemoteNotifications the application has stopped.

We have fixed this issue on our server and problem went off. Good luck.



回答2:

I was having the same stall issue.

It turns out that I was also getting a push notification parsing error on the console (like the one mentioned above by @CFIFOK).

"NSXPCConnection: ---" connection to service named com.apple.usernotifications.usernotificationservice: Exception caught during decoding of received message, dropping incoming message. Exception: Exception while decoding argument 0 (#2 of invocation): Exception: value for key 'NS.objects' was of unexpected class 'NSNumber'. Allowed classes are '{( NSString, NSArray )}'.

This was due to the "title-loc-args" : [3333] not accepting 3333 literally but accepting it as a string "title-loc-args" : ["3333"]. This little thing made my entire interface stall after I accessed the mentioned method isRegisteredForRemoteNotifications.

One thing to take into account is that this stall only happens on iOS 11. It works perfectly fine on iOS 12.0 (16A5366a).

In order to debug the error I used Pusher app (https://github.com/noodlewerk/NWPusher) and traced down the argument that was giving me the parsing error.

Hope this helps!