Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push notification not register to the app on iOS 13

Tags:

xcode

ios

ios13

I build my app and I put a breakpoint in didRegisterForRemoteNotificationsWithDeviceToken but it's not triggered. It works fine on other versions of iOS.

Is this a bug in iOS 13 or did I miss something new in iOS 13?

I use Xcode Beta 6 and iOS 13 beta 8.

like image 600
Halil İbrahim YILMAZ Avatar asked Aug 29 '19 12:08

Halil İbrahim YILMAZ


People also ask

Why push notification is not working for iOS?

You can fix an iPhone that's not getting notifications by restarting it or making sure notifications are turned on. You should also make sure your iPhone is connected to the internet so apps can receive notifications. If all else fails, you should try resetting the iPhone — just make sure to back it up first.

How do I enable push settings on iPhone 13?

Go to Settings and tap Notifications. Select an app under Notification Style. Under Alerts, choose the alert style that you want. If you turn on Allow Notifications, choose when you want the notifications delivered—immediately or in the scheduled notification summary.

Where do I find push notifications on my iPhone 13?

Find your notifications in Notification Center On the Lock Screen: Swipe up from the middle of the screen. On other screens: Swipe down from the top center. Then you can scroll up to see older notifications, if there are any.

Why is an app not showing up in notifications?

Reinstall the App or Wait for Updates One of the main reasons why your phone's notifications aren't working could be due to broken app updates. If your Android device is not getting notifications from one app in particular, it's possible that the developers have accidentally rolled out a buggy update.


1 Answers

The aforesaid problem could also be resolved by reconnecting wifi or switching between wifi and cellular data.

Moreover, following changes in iOS 13 affected push notification implementation.

Prior to iOS 13 many of us used to do

(deviceToken as NSData).description 
// Used to return as follows

"<965b251c 6cb1926d e3cb366f dfb16ddd e6b9086a 8a3cac9e 5f857679 376eab7C>"

let tokenData = deviceToken as NSData
let token = tokenData.description

let token = "\(deviceToken)".replacingOccurrences(of: " ", with: "")
                            .replacingOccurrences(of: "<", with: "")
                            .replacingOccurrences(of: ">", with: "")

In iOS 13 apple has changed the implementation of its description method for NSData class. So, it returns

"{length = 32, bytes = 0x965b251c 6cb1926d e3cb366f dfb16ddd ... 5f857679 376eab7c }" // in iOS 13.

Which ended up breaking push notifications implementation for many applications.

From now on, if you need to convert your push notification registration deviceToken into a Base16-encoded / hexadecimal string representation, you should do the following for Swift language

let deviceTokenString = deviceToken.map { String(format: "%02x", $0) 
}.joined()

For Objective C, use following code

- (NSString *)hexadecimalStringFromData:(NSData *)deviceToken {
  NSUInteger dataLength = deviceToken.length;
  if (dataLength == 0) {
    return nil;
  }

  const unsigned char *dataBuffer = (const unsigned char *)deviceToken.bytes;
  NSMutableString *hexString  = [NSMutableString stringWithCapacity:(dataLength * 2)];
  for (NSInteger index = 0; index < dataLength; ++index) {
    [hexString appendFormat:@"%02x", dataBuffer[index]];
  }
  return [hexString copy];
}

I came across a comprehensive article on the given topic https://nshipster.com/apns-device-tokens/

like image 149
Mihir Avatar answered Oct 19 '22 10:10

Mihir