即使更新内容的故事板,通知内容也不会被调用

时间:2019-07-10 09:31:42

标签: ios swift push-notification swift4.2 unnotificationscontentextension

我正在尝试通过图像进行推送通知。

以下是我从pushtry.com发送的推送

{
  "aps": {
    "alert": "message with meow image 11",
    "badge": 1,
    "sound": "default",
   "category": "DCCPush",
   "mutable-content": "1"
  },
  "Type": "product",
  "Id": 165,
  "imageURL": "https://i.stack.imgur.com/FXmv3.jpg"
}

推式影像正传给我,但问题是当我按下推式影像时,影像未显示。请参见下面的屏幕截图。

enter image description here

enter image description here

随着图片的不断推陈出新,我知道通知服务正在正常工作。 如果我输入错了,请纠正我。

现在,我觉得通知内容无法正常工作,因此我添加了带有橙色背景颜色的标签。

下面是我在图像的用户界面中所拥有的。

enter image description here

当您看到第二张图片时,我们没有看到这个橙色标签,这表示通知内容不起作用。

我正在关注this tutorial to make push with image.

以下是我在App Delegate中拥有的内容

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    let notificationDelegate = SampleNotificationDelegate()

    func configureNotification() {
        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
            center.delegate = notificationDelegate
            let openAction = UNNotificationAction(identifier: "OpenNotification", title: "clickToView".localized(), options: UNNotificationActionOptions.foreground)
            let deafultCategory = UNNotificationCategory(identifier: "DCCPush", actions: [], intentIdentifiers: [], options: [])
            center.setNotificationCategories(Set([deafultCategory]))
        } else {
            UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
        }
        UIApplication.shared.registerForRemoteNotifications()
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.


        self.configureNotification()

下面是我在SampleNotificationDelegate中所拥有的

import UIKit

import Foundation
import UserNotifications
import UserNotificationsUI

class SampleNotificationDelegate: NSObject , UNUserNotificationCenterDelegate {

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert,.sound])
    }

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        switch response.actionIdentifier {
        case UNNotificationDismissActionIdentifier:
            print("Dismiss Action")
        case UNNotificationDefaultActionIdentifier:
            print("====Open Action======")

            print("response===\(response)")

            let userInfo = response.notification.request.content.userInfo
            PushNotificationResponse(Data:userInfo as NSDictionary)


        case "Snooze":
            print("Snooze")
        case "Delete":
            print("Delete")
        default:
            print("default")
        }
        completionHandler()
    }

    func PushNotificationResponse(Data:NSDictionary){

        DispatchQueue.main.asyncAfter(deadline: .now() + 1) { // change 2 to desired number of seconds

             switch (Data["Type"] as! String) {

                case "product":

                    print("push details")

                    UserDefaults.standard.set(Data["Id"] as? Int, forKey: "pushId")
                    UserDefaults.standard.synchronize()

                    Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.prepareDataNotifications), userInfo: nil, repeats: false)
                    break
            default:
                break

            }
        }

    }

    @objc func prepareDataNotifications() {
        var mProduct : Products = Products()
        mProduct.id = UserDefaults.standard.integer(forKey: "pushId")
        Transition.openProductDetailsVCScreen2(withAnimation: true, productObject: mProduct)
    }

}

下面是内容中Info.plist的屏幕截图

enter image description here

知道我缺少什么吗?

1 个答案:

答案 0 :(得分:1)

问题是内容中的NotificationViewController没有标记为“初始视图控制器”

enter image description here

发生这种情况是因为我将旧项目的视图控制器复制到了当前项目中。 :)