Passer au contenu

Notifications push avec carrousel d'images sur iOS

Le protocole UNNotificationContentExtension fournit le point d’entrée pour une extension de contenu de notification, qui affiche une interface personnalisée pour les notifications de votre application.

1. Ajouter une extension de contenu de notification

Anchor link to

Dans Xcode, sélectionnez Fichier > Nouveau > Cible… (File > New > Target…)

Choisissez l’extension de contenu de notification (Notification Content Extension)

Nommez-la NotificationContentExtension

2. Ajouter du code à votre application

Anchor link to

Téléchargez PWNotificationExtension depuis Github et remplacez le PWNotificationContentExtension dans votre projet Xcode par le fichier correspondant de Github.

3. Catégorie de notification

Anchor link to

Ajoutez le code ci-dessous à votre fichier AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 10.0, *) {
let options: UNAuthorizationOptions = [.alert]
UNUserNotificationCenter.current().requestAuthorization(options: options) { (authorized, error) in
if authorized {
let categoryIdentifier = "PWNotificationCarousel"
let carouselNext = UNNotificationAction(identifier: "PWNotificationCarousel.next", title: "➡️ DROITE", options: [])
let carouselPrevious = UNNotificationAction(identifier: "PWNotificationCarousel.previous", title: "⬅️ GAUCHE", options: [])
let carouselCategory = UNNotificationCategory(identifier: categoryIdentifier, actions: [carouselNext, carouselPrevious], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([carouselCategory])
}
}
}
return true
}

4. Envoyer une notification push

Anchor link to

Catégorie iOS :

Envoyez une notification push avec le JSON APS :

JSON APS
{
"aps":{
"category":"PWNotificationCarousel"
}
}

Données personnalisées

Vous devez lister les URL des images séparées par une virgule ,

Exemple
{
"images":"image1.jpg, image2.jpg, image3.jpg"
}

Une fois la notification push reçue, vous devrez effectuer un appui long ou balayer vers la gauche et cliquer sur « Afficher » pour déplier la notification, selon votre version d’iOS.