Creating push notifications

If you ignore the complexities of setting up and hosting your own server, push notifications are actually pretty simple to create. Push notifications are always delivered using a JSON payload that your own server sends to APNS. APNS will then take care of sending your notification to the device that's intended to receive it. Let's look at a simple example of a JSON payload that could be used to inform users about a newsworthy event:

{  
  "aps": {  
    "alert": {  
      "title" : "Breaking news!",  
      "body" : "iOS 12 was released"  
    },  
    "badge" : 1  
  },  
  "item_id" : "1"  
}

This simple notification contains only a title and a body. The number 1 is added to the app icon to indicate the number of news items available. You'll notice that the notification content is placed in the aps dictionary. The aps dictionary contains the push notification's content. The item_id is not part of the aps dictionary on purpose; it's a custom property that can be read in the app so it can retrieve data about the news item from a local database, or fetch it from a backend using the ID.

If you want to send a silent push notification to trigger a background data refresh action, like CloudKit does, you should add the content-available key to the aps dictionary, specifying a value of 1. If you want to use a custom sound for your notification, you can add a sound key to aps. The value of this key should point to an audio file that's embedded in your app. Finally, a notification can be part of a category of notifications. If this is the case, add a category key to the aps dictionary, where the value is the category identifier. For an overview of all of the possible keys that can be added to the aps dictionary, have a look at Apple's Payload Key Reference documentation.