如果应用被强制退出,则处理IOS远程通知

时间:2018-09-24 04:16:59

标签: ios swift push-notification unusernotificationcenter

我处理了如下所示的代码段之类的远程通知,但是当用户强制使用该应用程序时,我无法启动该应用程序并重定向到特定页面。

$regex= "/\"offerId\" : (.*?), \"offerName\" : \"(.*?)\"/ms";

$subject='
"offerId" : 104, 
"offerName" : "Abcd", 
"alertText" : null,
"payoutType" : "REGISTER", 
"payoutDetails" : [ 
    { "propertyName" : "REGISTER",
      "propertyValue" : "1.0",
      "offerId" : 105, 
      "offerName" : "Abcdeeee",
      "alertText" : null,
      "payoutType" : "REGISTER", 
      "payoutDetails" : [ 
          { "propertyName" : "REGISTER",
            "propertyValue" : "1.0",
            "offerId" : 106,
            "offerName" : "Abcde", 
            "alertText" : null,
            "payoutType" : "REGISTER",
            "payoutDetails" : [
                { "propertyName" : "REGISTER",
                  "propertyValue" : "1.0"
                  ';
$subject = preg_replace('/\s+/', ' ', $subject);

preg_match_all($regex, $subject, $matches);

print_r( $matches);

我从附近某处看到,在appdelegate didfinishlaunchingwithoptions 中,我们可以添加一行:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {


        let userInfo = response.notification.request.content.userInfo
        let page = userInfo["page"] as! String


        if page == "message"{
            NotificationCenter.default.post(name: Notification.Name(rawValue: "Messages"), object: nil) }

但是我不知道如何从这本词典中提取消息信息

已更新我的通知响应:

    if let remoteNotification = launchOptions?[.remoteNotification] as?  [AnyHashable : Any] { 
}

2 个答案:

答案 0 :(得分:0)

您只需要像这样获取“页面”

if let remoteNotification = launchOptions?[.remoteNotification] as?  [AnyHashable : Any] {
            if let page = remoteNotification["page"] as? String {
                if page == "message" {

                }
            }
        }

注意-如果此操作无效,请显示您收到的应用内通知通知

答案 1 :(得分:0)

您需要通过didfinishlaunchingwithoptions委托方法访问launchOptions词典,然后如下所示访问通知有效内容。尝试先登录userInfo字典以查看其内容,然后访问通知的有效负载。

var userInfo = launchOptions [“ UIApplicationLaunchOptionsRemoteNotificationKey”]是否为? [AnyHashable:任意] var apsInfo = userInfo?[“ aps”]如? [AnyHashable:任何]