Search code examples
iosswiftpush-notificationswift4.2unnotificationscontentextension

Notification Content not get called even storyboard of content is updated


I am trying to make push notification with image.

Below is the push I am sending from 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.sstatic.net/FXmv3.jpg"
}

Push is coming to me with image but problem is when I pull down the push, image is not showing. Please see below screenshots.

enter image description here

enter image description here

As I am getting push with image, I understand Notification Service is working. Please correct me if I am wrong.

Now, I feel Notification Content is not working so what I did is added label with background color as Orange.

Below is what I have in UI for the image.

enter image description here

As you see second image we don't see this orange label means Notification Content is not working.

I am following this tutorial to make push with image.

Below is what I have in 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()

Below is what I have in 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)
    }

}

Below is the screenshot of Info.plist in Content

enter image description here

Any idea what I am missing?


Solution

  • The problem was NotificationViewController in Content was not marked as "Initial View Controller"

    enter image description here

    This happened because I copied view controller of old project to my current project. :)