gpt4 book ai didi

ios - 永远不会调用 didReceivelocalNotification

转载 作者:搜寻专家 更新时间:2023-10-30 22:15:01 24 4
gpt4 key购买 nike

我厌倦了这样做。让我告诉你发生了什么事。我使用的是 iOS 9.0 和 Xcode 7.3.1。

情况一:

我已经像这样在 didFinishLaunchingWithOptions 中注册了本地通知设置。

let settings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound] , categories: nil)
application.registerUserNotificationSettings(settings)

我的项目中有多个 Controller ,用户将按下其中一个中的一个按钮,我将通过调用 App Delegate 中的一个函数来安排通知。函数如下。

func activatingUserLocalNotification(timeIntervalSinceNow : NSDate?, alertBody : String, userInfo : [NSObject : AnyObject], region : CLRegion?)
{
let localNotification = UILocalNotification()
localNotification.fireDate = timeIntervalSinceNow
localNotification.timeZone = NSTimeZone.defaultTimeZone()
localNotification.alertBody = alertBody
localNotification.region = region
localNotification.regionTriggersOnce = false
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.userInfo = userInfo
localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}

现在我已经通过按下一个按钮设置了一个通知,它调用了上面的函数它成功地安排了通知。

我已经在所有这些方法中设置了断点。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
// Break Point
return true
}

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {

// Break Point
}

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {

// Break Point
}

func application(application: UIApplication, didReceivelocalNotification notification: UILocalNotification)
{
// Break Point
}

当应用程序处于 Foreground 时等待一段时间,等待更多时间。没啥事儿。我认为该应用程序会启动通知,并且会调用其中一个方法,这样我就可以在应用程序内显示通知。但什么也没发生。

情况二:

现在我尝试了同样的方法,现在我通过转到另一个应用程序并使用它一段时间来最小化应用程序 [App is in Background state ]。现在通知正常启动了,就像我告诉过你的那样,我在几乎所有方法中都设置了断点,但当我单击通知时,没有一个方法被调用。但是它调用了 applicationWillEnterForeground,如果没有 launchOptions,这个方法该怎么办。

我已经为此奋斗了两天,对发生的事情一无所知。

推送通知在 [应用程序内部和外部] 都可以正常工作

让我知道你的想法?请。

我如何从 Controller 调用此函数 activatingUserLocalNotification

 func setLocalNotificationForDistance(id : String, name : String, location : CLLocationCoordinate2D, radius : Double)
{
let alertBody = "Hello \(name)"
let dict : [NSObject : AnyObject] = ["aps" : ["alert" : alertBody], “id” : id]
let region = CLCircularRegion(center: location, radius: radius, identifier: id)

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.activatingUserLocalNotification(nil, alertBody: alertBody, userInfo: dict, region: region)
}

应用委托(delegate)

//
// AppDelegate.swift
//
//

import UIKit
import FBSDKCoreKit
import SVProgressHUD
import Alamofire
import GoogleMaps

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

//MARK: Local Variables

var window: UIWindow?

static let UpdateRootNotification = "UpdateRootNotification"
static let ShowMainUINotification = "ShowMainUINotification"

//MARK: Application Life Cycle

// Did Finish Launching

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{

let settings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound] , categories: nil)
application.registerUserNotificationSettings(settings)

return true
}

// Foreground

func applicationWillEnterForeground(application: UIApplication) {


}

// Did Become Active

func applicationDidBecomeActive(application: UIApplication) {
FBSDKAppEvents.activateApp()
application.applicationIconBadgeNumber = 0 // clear badge icon
}

// Did Enter Background

func applicationDidEnterBackground(application: UIApplication) {
}

// Opened Via Shortcut

func application(application: UIApplication, performActionForShortcutItem
shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {

}

// Continue User Activity

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity,
restorationHandler: ([AnyObject]?) -> Void) -> Bool {

return true
}



//MARK: Local Notification

func activatingUserLocalNotification(timeIntervalSinceNow : NSDate?, alertBody : String, userInfo : [NSObject : AnyObject], region : CLRegion?)
{
let localNotification = UILocalNotification()
localNotification.fireDate = timeIntervalSinceNow
localNotification.timeZone = NSTimeZone.defaultTimeZone()
localNotification.alertBody = alertBody
localNotification.region = region
localNotification.regionTriggersOnce = false
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.userInfo = userInfo
localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {


}

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {


}

func application(application: UIApplication, didReceivelocalNotification notification: UILocalNotification)
{
application.applicationIconBadgeNumber = 0

UIApplication.sharedApplication().presentLocalNotificationNow(notification)

}

// MARK: Push Notification

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

if API.sharedInstance.isLoggedIn() {
NSUserDefaults.standardUserDefaults().setObject(deviceToken, forKey: "push_token")
NSUserDefaults.standardUserDefaults().synchronize()

API.sharedInstance.registerDeviceToken(deviceToken)
}
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
application.registerForRemoteNotifications()
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
EventTracker.trackEventWithCategory("APN", action: "Registraion", label: "Failed")
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
APNSManager.sharedInstance.handlePushNotification(userInfo)
}

//MARK: Open URL

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL:url,sourceApplication:sourceApplication,annotation:annotation)
}

}

最佳答案

这有点滑稽,但是改变一下:

func application(application: UIApplication, didReceivelocalNotification notification: UILocalNotification)

收件人:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)

注意大写的 L。来自文档:

enter image description here

此外:期望此委托(delegate)方法仅在您的应用处于事件状态时被调用。由于您的应用程序处于事件状态,因此在这种情况下不应显示任何通知。您可以使用 didReceiveLocalNotification 委托(delegate)方法来处理它。

关于ios - 永远不会调用 didReceivelocalNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39073696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com