HomeiOS DevelopmentPush Notifications Console: Apple’s Recreation-Altering New Device

Push Notifications Console: Apple’s Recreation-Altering New Device


At WWDC ’23, Apple introduced the Push Notifications Console, a much-needed new device to simplify creating and testing your push notifications.

For years, builders have struggled with push notifications. Every developer needed to write customized scripts to not solely generate the suitable payload, but additionally deal with the authentication to APNs. In later years, Apple simplified the method by permitting the simulator to set off a notification by dropping in a JSON payload.

Due to the Push Notifications Console, now you can say goodbye to these clunky, outdated strategies. However first, you’ll check out how push notifications have labored up to now.

Observe: If it’s good to brush up in your push notification expertise, take a look at our e-book: Push Notifications by Tutorials. It covers every thing it’s good to learn about wealthy media notifications, notification actions, grouped notifications and extra.

Sending Push Notifications — the Outdated Approach

Till now, if you wanted push notifications, you’ve most likely written a script with code like this:

perform generateAuthenticationHeader()
{
  $header = base64_encode(json_encode(['alg' => 'ES256', 'kid' => AUTH_KEY_ID]));
  $claims = base64_encode(json_encode(['iss' => TEAM_ID, 'iat' => time()]));

  $pkey = openssl_pkey_get_private('file://' . AUTH_KEY_PATH);
  openssl_sign("$header.$claims", $signature, $pkey, 'sha256');

  $signed = base64_encode($signature);

  return "$header.$claims.$signed";
}

perform sendNotifications($token, $debug) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($GLOBALS['payload']));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, [
      'apns-topic: ' . BUNDLE_ID,
      'authorization: bearer ' . generateAuthenticationHeader(),
      'apns-push-type: alert'
  ]);

  $server = $debug ? 'api.growth' : 'api';
  $url = "https://$server.push.apple.com/3/gadget/$token";
  curl_setopt($ch, CURLOPT_URL, "{$url}");

  $response = curl_exec($ch);
  if ($response === false) {
    echo("curl_exec failed: " . curl_error($ch));
    proceed;
  }

  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  if ($code === 400 || $code === 410) {
    $json = @json_decode($response);
    if ($json->motive === 'BadDeviceToken') {
        // Do one thing right here.
    }
  }

  curl_close($ch);
}

Whereas that works, it isn’t fairly.

Varied apps from the Mac App Retailer additionally promise to make push notifications simpler, however they continuously don’t sustain with modifications to the APN’s packet format and headers chances are you’ll want to use.

Now, you’ll check out the Push Notifications Console and the way it improves push notification testing.

The New Push Notifications Console

To strive Apple’s new device, level Safari to the CloudKit Console and click on the brand new Push Notifications merchandise:

bordered

In case your app isn’t configured to obtain push notifications, you’ll have the choice to allow them:

bordered

When you’ve got multiple app configured within the developer portal, choose the suitable one from the drop-down on the top-left of the web page.

Clicking Create New Notification will current you with a display permitting you to simply configure the notification you want to ship:

bordered

The brand new console makes it easy to supply all of the required configuration settings. It additionally clearly reveals the 4 objects that should be despatched as HTTP headers, versus a part of the payload.

Configuring the Payload

Apple offers a pleasant set of defaults for the payload configuration, enabling you to enter easy messages quickly. When you’ve got already ready a customized JSON payload, you may simply toggle the JSON View change, then paste in your payload instantly:

bordered

Not solely does the console validate that you simply’ve supplied correct JSON, but it surely additionally notifies you if the payload doesn’t appear to evolve to the apns-push-type you chose.

You possibly can toggle the JSON View on and off to modify between codecs. A great technique is to make use of the default view to enter a lot of the knowledge, then change to the JSON view when getting into an array of things to cut back the quantity of mouse-clicking it’s good to do.

Debugging With the Supply Log

One other nice function now out there to builders is the supply log. After sending your notification, this function will instantly current you with a web page exhibiting the main points of the despatched notification in addition to the log:

bordered

The logs will assist you to establish not solely whether or not or not the push notification was profitable, but additionally why it wasn’t delivered on failure.

By no means earlier than have builders had such a easy methodology of debugging push notification points!

The place to Go From Right here?

This was only a temporary overview of latest push notification options. To study extra:

We hope you loved this fast take a look at what’s new in push notifications. When you’ve got any questions or feedback, please click on the Feedback hyperlink to affix the discussion board dialogue beneath!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments