0

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

以下是我从 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"
}

推送带着图片来找我,但问题是当我拉下推送时,图片没有显示。请看下面的截图。

在此处输入图像描述

在此处输入图像描述

当我使用图像推送时,我了解通知服务正在工作。 如果我错了,请纠正我。

现在,我觉得通知内容不起作用,所以我所做的是添加背景颜色为橙色的标签。

下面是我在 UI 中的图像。

在此处输入图像描述

正如您看到的第二张图片,我们没有看到这个橙色标签意味着通知内容不起作用。

我正在按照本教程使用图像进行推送。

以下是我在 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)
    }

}

下面是 Content 中 Info.plist 的截图

在此处输入图像描述

知道我缺少什么吗?

4

1 回答 1

2

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

在此处输入图像描述

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

于 2019-07-13T09:05:56.403 回答