【问题标题】:iOS handling Push notification tap when app is active当应用程序处于活动状态时,iOS 处理推送通知点击
【发布时间】:2019-07-25 07:11:52
【问题描述】:

当应用程序处于活动状态时,如何处理推送通知点击? didReceiveRemoteNotification userInfo: [AnyHashable : Any] 在 iOS 设备接收到推送通知时调用,并且在用户点击通知时调用相同的函数。我如何在这个函数中区分这个函数是如何被调用的?我正在使用OneSignal 的推送通知,以防有必要了解问题。

【问题讨论】:

  • 当您的应用将ResignActive时保存标志,在didReceiveRemoteNotification中检查它

标签: ios push-notification onesignal


【解决方案1】:

OneSingal 有闭包,可以通知您有关通知和用户操作的信息。我就是这样用的

func initOneSignalNotifications(withLaunchOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) {
        let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false]

        OneSignal.initWithLaunchOptions(launchOptions, appId: Constants.oneSignalKey, handleNotificationReceived: { (receivedNotification) in

            //Notification is received

        }, handleNotificationAction: { (notificationResult) in
            //Notification was tapped
        }, settings: onesignalInitSettings)

        OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification;
        OneSignal.promptForPushNotifications(userResponse: { accepted in
            FileHandler.log(message: "Notification permission granted: \(accepted)", tag: .application, logLevel: .info)
        })
    }

这里的inFocusDisplayType 表示当您的应用打开时,OneSignal 仍会显示通知。

【讨论】:

    【解决方案2】:

    如果你的目标是iOS 10以上,你可以处理点击。

    func userNotificationCenter ( _ center : UNUserNotificationCenter, didReceive
    response : UNNotificationResponse, withCompletionHandler completionHandler :
    @escaping () -> Void ) {
            // perform notification received/click action as per third party SDK as per their document
        }
    }
    

    否则,您需要使用didReceiveRemoteNotification userInfo: [AnyHashable : Any] 中的标志来管理它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多