发送本地推送通知

Send local Push Notification

我正在尝试使用 swift 立即在本地发送推送通知(阅读:未来未安排)。

我正在努力

          var localNotification:UILocalNotification = UILocalNotification()
        localNotification.alertAction = "Testing notifications on iOS8"
        localNotification.alertBody = "Some text here"
        localNotification.fireDate = NSDate(timeIntervalSinceNow: 1)
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

已获得发送推送通知的权限。

虽然正在调用代码,但没有发送推送通知。我做错了什么?

谢谢!

您已确保为您的应用注册通知(例如在AppDelegate中)。
在 Objective-C 中,我设法使用以下方法做到了这一点:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000


    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];

#else

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

#endif

然后你的代码就会被执行。

另一件事是,如果您在前台使用应用程序,您可能根本看不到它们 - 尝试向下滑动打开 Notification Center

您的问题可能是您没有看到通知,但它确实存在。只需打开通知中心,它应该在那里:

要检查这一点,请在您的 AppDelegate 中实施 didReceiveLocalNotification 方法:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
        println("Notification done")
    }

然后您可以处理您的通知并显示弹出窗口或任何您想要的内容。

您只需将 timeZone 设置为 localTimeZone

The date specified in fireDate is interpreted according to the value of this property. If you specify nil (the default), the fire date is interpreted as an absolute GMT time, which is suitable for cases such as countdown timers. If you assign a valid NSTimeZone object to this property, the fire date is interpreted as a wall-clock time that is automatically adjusted when there are changes in time zones; an example suitable for this case is an an alarm clock.

// 应用委托

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


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

        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil))
        return true
    }

/// 视图控制器

import UIKit

class ViewController: UIViewController {
    var localNotification = UILocalNotification()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        localNotification.alertAction = "Testing notifications on iOS8"
        localNotification.alertBody = "Some text here"
        localNotification.timeZone = NSTimeZone.localTimeZone()

        localNotification.fireDate = NSDate(timeIntervalSinceNow: 1)
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}