Skip to content

Instantly share code, notes, and snippets.

@hashier
Created July 2, 2017 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hashier/cc09d1c5bcfc99d4e283b34b2aa8e826 to your computer and use it in GitHub Desktop.
Save hashier/cc09d1c5bcfc99d4e283b34b2aa8e826 to your computer and use it in GitHub Desktop.
Just +1 the badge and add '[modified]' to the title of the notification
Connected to APNS
array(1) {
["aps"]=>
array(4) {
["alert"]=>
string(13) "badge testing"
["sound"]=>
string(7) "default"
["badge"]=>
int(98)
["mutable-content"]=>
int(1)
}
}
Message successfully delivered
119
//
// NotificationService.swift
// quickTest
//
// Created by Christopher Loessl on 2017-07-02.
// Copyright © 2017. All rights reserved.
//
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
bestAttemptContent.badge = NSNumber(value: bestAttemptContent.badge!.intValue + 1)
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
bestAttemptContent.badge = 42
contentHandler(bestAttemptContent)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment