【问题标题】:Not able to perform action on push notification custom buttons无法对推送通知自定义按钮执行操作
【发布时间】:2018-09-27 06:55:51
【问题描述】:

我正在开发一个涉及设备到设备推送通知的 iOS 应用程序。在前景和背景状态下,我能够接收通知并能够在各自的自定义按钮(接受和拒绝)中执行各自的操作。在提到的两个状态下一切正常。但是在终止/终止状态下,虽然我能够收到通知,但我无法在单击自定义按钮(接受和拒绝)时执行操作。你们能帮帮我吗?

//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()
}

【问题讨论】:

  • 在我的情况下,我在杀死状态下没有收到通知?你是怎么做到的?我在我的有效负载中设置了 content-available :1 。虽然它没有在杀死状态下显示通知。
  • 你能告诉我你的有效载荷吗?
  • 我也面临同样的挑战?你解决了吗?

标签: ios swift xcode firebase push-notification


【解决方案1】:

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

【讨论】:

  • 我已经用代码更新了我的帖子。请仔细查看。
【解决方案2】:

在didFinishLaunchingWithOptionsdelegate 中添加以下条件

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

你可以在里面调用didReceive委托方法。

【讨论】:

  • 我不必点击通知。我必须点击动作类别(接受和拒绝)
  • 好的,当应用程序在后台时,如果您点击接受,将首先调用哪个方法?不是 didReceive 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 2014-10-22
相关资源
最近更新 更多