无法对推送通知自定义按钮执行操作

时间:2018-09-27 06:55:51

标签: ios swift xcode firebase push-notification

我正在使用涉及设备到设备推送通知的iOS应用程序。在“前台”和“后台”状态下,我可以在相应的自定义按钮(接受和拒绝)中接收通知并执行相应的操作。在上述两个状态下,一切正常。但是在终止/终止状态下,虽然我可以接收通知,但是我无法在单击自定义按钮上执行操作(接受和拒绝)。你们能帮我吗?Notification screenshot with Custom buttons Accept and Reject

//Notification action button function
func setActionCategories(){
    let acceptAction = UNNotificationAction(
        identifier: NAString().notificationAcceptIdentifier(),
        title: NAString().accept().capitalized,
        options: [.init(rawValue: 0)])

    let rejectAction = UNNotificationAction(
        identifier: NAString().notificationRejectIdentifier(),
        title: NAString().reject().capitalized,
        options: [.init(rawValue: 0)])

    let actionCategory = UNNotificationCategory(
        identifier: NAString().notificationActionCategory(),
        actions: [acceptAction,rejectAction],
        intentIdentifiers: [],
        options: [.customDismissAction])

    UNUserNotificationCenter.current().setNotificationCategories(
        [actionCategory])
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo

    //Here we are performing Action on Notification Buttons & We created this buttons in  "setActionCategories" function.
    if response.notification.request.content.categoryIdentifier == NAString().notificationActionCategory() {

        //Created Firebase reference to get currently invited visitor by E-Intercom
        var gateNotificationRef : DatabaseReference?
        gateNotificationRef = GlobalUserData.shared.getUserDataReference().child(Constants.FIREBASE_CHILD_GATE_NOTIFICATION).child(userUID).child(guestType!).child(guestUID!)

        //Performing accept & reject on click of recently invited visitor by E-Intercom from Notification view.
        switch response.actionIdentifier {

        //If Accept button will pressed
        case NAString().notificationAcceptIdentifier():
            gateNotificationRef?.child(NAString().status()).setValue(NAString().accepted())
            }
            break

        //If Reject button will pressed
        case NAString().notificationRejectIdentifier():                                                         gateNotificationRef?.child(NAString().status()).setValue(NAString().rejected())
            break

        default:
            break
        }
    }
    UIApplication.shared.applicationIconBadgeNumber = 0
    completionHandler()
}

2 个答案:

答案 0 :(得分:0)

嗨Ashish您能为我们提供一些代码以更好地为您提供帮助吗?在其中应该有一个完成处理程序,您可以在其中添加一个动作函数。然后,您可以执行所需的任何操作。

答案 1 :(得分:0)

didFinishLaunchingWithOptions委托中添加以下条件

if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil {
    // Do what you want to do when remote notification is tapped.
}

您可以在其中调用didReceive委托方法。