简体   繁体   中英

Send local Push Notification

I am trying to send a push notification locally with swift, immediatelly (read: not scheduled for the future).

I am trying

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

with permission to send push notifications having been already granted.

Though the code is getting called, no push notification is sent. What am I doing wrong?

Thanks!

you just have to set the timeZone as 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.

// App delegate

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
    }

/// View Controller

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.
    }


}

You have make sure to register notifications for your app (for example in AppDelegate ).
In Objective-C I have managed to do this using:

#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

Then your code will get executed.

Another thing is, that you may simply not see them if you're using an app in foreground - try opening Notification Center by swiping down.

Your problem might be that you don't see the notification, but it's actually there. Just open the notification-center and it should be there:

在此处输入图片说明

To check that, implement the didReceiveLocalNotification method into your AppDelegate:

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

Then you can handle your notification and show a PopUp or whatever you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM