Skip to main content
本文に 235 文字追加
ソース リンク
kishikawa katsumi
  • 8,520
  • 2
  • 18
  • 40

お世話になります。
SwiftSwift2.3記述した UserNotification コードUserNotificationコード(下記)で userNotificationCenter がuserNotificationCenterが呼ばれません。
基本的に同等(と思われる) Swift3 でSwift3では動作します。基本的というのは、Swift2.3 と Swift3 でシンタックスが異なり、Xcodeの補完に従って変更しています。ここで誤っているのかもしれませんが、、、
よろしくお願いします。
//
import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    
    var window: UIWindow?
 
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        if #available(iOS 10.0, *) {
            // iOS 10
            let center = UNUserNotificationCenter.currentNotificationCenter()

            center.requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: { (granted, error) in
                if error != nil {
                    return
                }
                
                if granted {
                    debugPrint("通知許可")
                    
                    center.delegate = self
                    
                    let content = UNMutableNotificationContent()
                    content.title = "Title"
                    content.subtitle = "Subtitle" // 新登場!
                    content.body = "Body"
                    content.sound = UNNotificationSound.defaultSound()
                    
                    // 5秒後に発火
                    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
                    let request = UNNotificationRequest(identifier: "FiveSecond",
                                                        content: content,
                                                        trigger: trigger)
                    
                    // ローカル通知予約
                    center.addNotificationRequest(request, withCompletionHandler: nil)
                    
                    center.getPendingNotificationRequestsWithCompletionHandler { (requests: [UNNotificationRequest]) in
                        print(">>>>> \(requests)")
                    }

                } else {
                    debugPrint("通知拒否")
                }
            })
            
        } else {
            // <= iOS 9

// let settings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil)
// UIApplication.shared.registerUserNotificationSettings(settings)
}

//            let settings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil)
//            UIApplication.shared.registerUserNotificationSettings(settings)
        }
        
        return true
    }
        
    @available(iOS 10.0, *)
    func userNotificationCenter(center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
        
        // ...
        let identifier = notification.request.identifier
        debugPrint("userNotificationCenter \(identifier)")
   }
}

}
}

お世話になります。
Swift2.3記述した UserNotification コード(下記)で userNotificationCenter が呼ばれません。
基本的に同等(と思われる) Swift3 では動作します。基本的というのは、Swift2.3 と Swift3 でシンタックスが異なり、Xcodeの補完に従って変更しています。ここで誤っているのかもしれませんが、、、
よろしくお願いします。
//
import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow?
 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    
    if #available(iOS 10.0, *) {
        // iOS 10
        let center = UNUserNotificationCenter.currentNotificationCenter()

        center.requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: { (granted, error) in
            if error != nil {
                return
            }
            
            if granted {
                debugPrint("通知許可")
                
                center.delegate = self
                
                let content = UNMutableNotificationContent()
                content.title = "Title"
                content.subtitle = "Subtitle" // 新登場!
                content.body = "Body"
                content.sound = UNNotificationSound.defaultSound()
                
                // 5秒後に発火
                let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
                let request = UNNotificationRequest(identifier: "FiveSecond",
                                                    content: content,
                                                    trigger: trigger)
                
                // ローカル通知予約
                center.addNotificationRequest(request, withCompletionHandler: nil)
                
                center.getPendingNotificationRequestsWithCompletionHandler { (requests: [UNNotificationRequest]) in
                    print(">>>>> \(requests)")
                }

            } else {
                debugPrint("通知拒否")
            }
        })
        
    } else {
        // <= iOS 9

// let settings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil)
// UIApplication.shared.registerUserNotificationSettings(settings)
}

    return true
}
    
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
    
    // ...
    let identifier = notification.request.identifier
    debugPrint("userNotificationCenter \(identifier)")

}
}

Swift2.3記述したUserNotificationコード(下記)でuserNotificationCenterが呼ばれません。
基本的に同等(と思われる)Swift3では動作します。基本的というのは、Swift2.3 と Swift3 でシンタックスが異なり、Xcodeの補完に従って変更しています。ここで誤っているのかもしれませんが、、、

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    
    var window: UIWindow?
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        if #available(iOS 10.0, *) {
            // iOS 10
            let center = UNUserNotificationCenter.currentNotificationCenter()

            center.requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: { (granted, error) in
                if error != nil {
                    return
                }
                
                if granted {
                    debugPrint("通知許可")
                    
                    center.delegate = self
                    
                    let content = UNMutableNotificationContent()
                    content.title = "Title"
                    content.subtitle = "Subtitle" // 新登場!
                    content.body = "Body"
                    content.sound = UNNotificationSound.defaultSound()
                    
                    // 5秒後に発火
                    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
                    let request = UNNotificationRequest(identifier: "FiveSecond",
                                                        content: content,
                                                        trigger: trigger)
                    
                    // ローカル通知予約
                    center.addNotificationRequest(request, withCompletionHandler: nil)
                    
                    center.getPendingNotificationRequestsWithCompletionHandler { (requests: [UNNotificationRequest]) in
                        print(">>>>> \(requests)")
                    }

                } else {
                    debugPrint("通知拒否")
                }
            })
            
        } else {
            // <= iOS 9
//            let settings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil)
//            UIApplication.shared.registerUserNotificationSettings(settings)
        }
        
        return true
    }
        
    @available(iOS 10.0, *)
    func userNotificationCenter(center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
        
        // ...
        let identifier = notification.request.identifier
        debugPrint("userNotificationCenter \(identifier)")
   }
}
ソース リンク
THo
  • 29
  • 1
  • 4

Swift2.3 で UNUserNotification (iOS10) のdelegateメソッドが呼ばれない

お世話になります。
Swift2.3 で記述した UserNotification コード(下記)で userNotificationCenter が呼ばれません。
基本的に同等(と思われる) Swift3 では動作します。基本的というのは、Swift2.3 と Swift3 でシンタックスが異なり、Xcodeの補完に従って変更しています。ここで誤っているのかもしれませんが、、、
よろしくお願いします。
//
import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    
    if #available(iOS 10.0, *) {
        // iOS 10
        let center = UNUserNotificationCenter.currentNotificationCenter()

        center.requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: { (granted, error) in
            if error != nil {
                return
            }
            
            if granted {
                debugPrint("通知許可")
                
                center.delegate = self
                
                let content = UNMutableNotificationContent()
                content.title = "Title"
                content.subtitle = "Subtitle" // 新登場!
                content.body = "Body"
                content.sound = UNNotificationSound.defaultSound()
                
                // 5秒後に発火
                let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
                let request = UNNotificationRequest(identifier: "FiveSecond",
                                                    content: content,
                                                    trigger: trigger)
                
                // ローカル通知予約
                center.addNotificationRequest(request, withCompletionHandler: nil)
                
                center.getPendingNotificationRequestsWithCompletionHandler { (requests: [UNNotificationRequest]) in
                    print(">>>>> \(requests)")
                }

            } else {
                debugPrint("通知拒否")
            }
        })
        
    } else {
        // <= iOS 9

// let settings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil)
// UIApplication.shared.registerUserNotificationSettings(settings)
}

    return true
}
    
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
    
    // ...
    let identifier = notification.request.identifier
    debugPrint("userNotificationCenter \(identifier)")

}
}