15.9 C
New York
Sunday, March 30, 2025

ios – Notification Service Extension cannot edit notifications


i’ve added Notification Service Extension to my mission and need to edit the notifications after they arrived to the appliance:

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 material.mutableCopy() as? UNMutableNotificationContent)
        
        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content material right here...
            bestAttemptContent.title = "(bestAttemptContent.title) [modified]"
            bestAttemptContent.physique = "(bestAttemptContent.physique) [modified]"
            
            contentHandler(bestAttemptContent)
        }
    }
    
    override func serviceExtensionTimeWillExpire() {
        // Known as simply earlier than the extension will probably be terminated by the system.
        // Use this as a chance to ship your "finest try" at modified content material, in any other case the unique push payload will probably be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }

}

that is how I name notifications(for testing):

var apn = require('apn');

var apnProvider = new apn.Supplier({  
  token: {
    key: '*****.p8',
    keyId: '*******',
    teamId: '*******'
  },
  manufacturing: false
});

var deviceToken = '*******';

var notification = new apn.Notification();

notification.subject="*******";
notification.expiry = Math.flooring(Date.now() / 1000) + 3600;
notification.badge = 1;
notification.sound = 'Notification_1.wav';

notification.mutableContent = true;
notification.alert = {
  title: "That is the title",
  physique: "That is the physique"
};

notification.payload = {
  kind: "*******",
  from: "*******",
  groupId: "*******",
  fetchData: {
    id: "*******",
    kind: "*******"
  }
};

console.log(notification.compile());

apnProvider.ship(notification, deviceToken).then(operate(consequence) {
  console.log(JSON.stringify(consequence, null, 2));
  course of.exit(0);
});

The issue is that I can not see the [modified] textual content within the notification. Do you’ve any concept what the issue is or what could cause it?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles