i have added Notification Service Extension to my project and want to edit the notifications after they arrived to the application:
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.body = "\(bestAttemptContent.body) [modified]"
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
this is how I call notifications(for testing):
var apn = require('apn');
var apnProvider = new apn.Provider({
token: {
key: '*****.p8',
keyId: '*******',
teamId: '*******'
},
production: false
});
var deviceToken = '*******';
var notification = new apn.Notification();
notification.topic = '*******';
notification.expiry = Math.floor(Date.now() / 1000) + 3600;
notification.badge = 1;
notification.sound = 'Notification_1.wav';
notification.mutableContent = true;
notification.alert = {
title: "This is the title",
body: "This is the body"
};
notification.payload = {
type: "*******",
from: "*******",
groupId: "*******",
fetchData: {
id: "*******",
type: "*******"
}
};
console.log(notificationpile());
apnProvider.send(notification, deviceToken).then(function(result) {
console.log(JSON.stringify(result, null, 2));
process.exit(0);
});
The problem is that I can't see the [modified] text in the notification. Do you have any idea what the problem is or what can cause it?
i have added Notification Service Extension to my project and want to edit the notifications after they arrived to the application:
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.body = "\(bestAttemptContent.body) [modified]"
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
this is how I call notifications(for testing):
var apn = require('apn');
var apnProvider = new apn.Provider({
token: {
key: '*****.p8',
keyId: '*******',
teamId: '*******'
},
production: false
});
var deviceToken = '*******';
var notification = new apn.Notification();
notification.topic = '*******';
notification.expiry = Math.floor(Date.now() / 1000) + 3600;
notification.badge = 1;
notification.sound = 'Notification_1.wav';
notification.mutableContent = true;
notification.alert = {
title: "This is the title",
body: "This is the body"
};
notification.payload = {
type: "*******",
from: "*******",
groupId: "*******",
fetchData: {
id: "*******",
type: "*******"
}
};
console.log(notificationpile());
apnProvider.send(notification, deviceToken).then(function(result) {
console.log(JSON.stringify(result, null, 2));
process.exit(0);
});
The problem is that I can't see the [modified] text in the notification. Do you have any idea what the problem is or what can cause it?
Share Improve this question asked Mar 30 at 16:01 YosiFZYosiFZ 7,91023 gold badges119 silver badges230 bronze badges1 Answer
Reset to default -1try
if let content = bestAttemptContent {
content.title = "\(bestAttemptContent.title) [modified]"
content.body = "\(bestAttemptContent.body) [modified]"
contentHandler(content)
}