package your.package.nameimport androidx.annotation.Keep;import com.onesignal.notifications.IActionButton;import com.onesignal.notifications.IDisplayableMutableNotification;import com.onesignal.notifications.INotificationReceivedEvent;import com.onesignal.notifications.INotificationServiceExtension;@Keep // Keep is required to prevent minification from renaming or removing your classpublic class NotificationServiceExtension implements INotificationServiceExtension { @Override public void onNotificationReceived(INotificationReceivedEvent event) { IDisplayableMutableNotification notification = event.getNotification(); if (notification.getActionButtons() != null) { for (IActionButton button : notification.getActionButtons()) { // you can modify your action buttons here } } /* Add customizations here. See examples below for additional methods to modify the notification*/ }}
@Keep 어노테이션은 ProGuard/R8이 축소 과정에서 INotificationServiceExtension을 구현하는 클래스의 이름을 변경하거나 제거하지 못하도록 방지합니다.
다음은 위의 템플릿 Notification Service Extension 클래스에서 구현할 수 있는 일반적인 예시입니다.
알림 표시 방지
사용자 지정 필드 추가
알림 색상 및 아이콘 변경
event.preventDefault()를 호출하면 알림이 자동으로 표시되지 않으며, 수동으로 표시하거나 완전히 표시하지 않도록 결정할 수 있습니다.
event.preventDefault();//Do some async work, then decide to show or dismissnew Thread(() -> { try { Thread.sleep(1000); } catch (InterruptedException ignored) {} //Manually show the notification event.getNotification().display();}).start();
event.preventDefault()를 호출한 후 display()를 호출하지 않으면 알림이 아무런 경고 없이 자동으로 삭제됩니다. 자세한 내용은 중복 알림을 참조하세요.
didReceive(_:withContentHandler:) 오버라이드는 OneSignalExtension.didReceiveNotificationExtensionRequest를 호출합니다. 이 메서드가 호출되기 전에 bestAttemptContent를 읽거나 수정할 수 있습니다.이 예시에서는 다음 데이터와 함께 알림을 보냅니다:
userInfo의 custom 딕셔너리 내 a 키를 통해 OneSignalNotificationServiceExtension 내에서 이 추가 data에 액세스합니다:
if let bestAttemptContent = bestAttemptContent { if let customData = bestAttemptContent.userInfo["custom"] as? [String: Any], let additionalData = customData["a"] as? [String: Any] { if let jsonData = try? JSONSerialization.data(withJSONObject: additionalData, options: .prettyPrinted), let jsonString = String(data: jsonData, encoding: .utf8) { print("The additionalData dictionary in JSON format:\n\(jsonString)") } else { print("Failed to convert additionalData to JSON format.") } } if let messageData = bestAttemptContent.userInfo["aps"] as? [String: Any], let apsData = messageData["alert"] as? [String: Any], let body = apsData["body"] as? String, let title = apsData["title"] as? String { print("The message contents is: \(body), message headings is: \(title)") } else { print("Unable to retrieve apsData") } OneSignalExtension.didReceiveNotificationExtensionRequest(self.receivedRequest, with: bestAttemptContent, withContentHandler: self.contentHandler)}
콘솔 출력 예시:
The additionalData dictionary in JSON format:{ "additional_data_key_1" : "value_1", "additional_data_key_2" : "value_2"}The message contents is: The message contents, message headings is: The message title
NotificationService.m 또는 NotificationService.swift를 열고 전체 파일 내용을 아래 코드로 바꾸세요. Extension이 실행 중인지 확인하는 데 도움이 되는 로깅을 추가합니다.YOUR_BUNDLE_ID를 실제 Bundle ID로 바꾸세요.
iOS에서 Notification Service Extension이 실행되지 않는 이유는 무엇인가요?Extension은 mutable-content가 설정된 경우에만 실행됩니다. OneSignal은 첨부 파일이나 액션 버튼이 있는 알림에 대해 자동으로 이를 설정합니다. Xcode 설정이 문제 해결 섹션의 요구 사항과 일치하는지 확인하세요.Android에서 알림 표시를 방지할 수 있나요?네, event.preventDefault()를 호출하여 알림을 억제할 수 있습니다. 나중에 event.getNotification().display()를 호출하여 표시하거나 호출하지 않고 자동으로 삭제할 수 있습니다. 자세한 내용은 중복 알림을 참조하세요.Android에서 @Keep 어노테이션이 필요한가요?네, @Keep 어노테이션은 축소 과정에서 ProGuard/R8이 INotificationServiceExtension을 구현하는 클래스의 이름을 변경하거나 제거하지 못하도록 방지합니다.