当按钮被点击后如何发送推送通知?

3

我目前有一个已经设置好接收推送通知的iOS应用程序。现在我想要实现的是:当用户按下“加入”按钮后,我想要向他们发送一条“欢迎”的推送通知。你有什么办法实现吗?

2个回答

3

It's pretty easy.

AppDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Sound, .Badge], categories: nil))
    return true
}

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    print("Local notification received (tapped, or while app in foreground): \(notification)")
}

那么在你的操作中:

@IBAction func welcomeMe(sender: AnyObject) {
    let notification = UILocalNotification()
    notification.alertBody = "Welcome to the app!" // text that will be displayed in the notification

    notification.fireDate = NSDate(timeIntervalSinceNow: 2)
    notification.soundName = UILocalNotificationDefaultSoundName

    notification.userInfo = ["title": "Title", "UUID": "12345"]         
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

现在如果应用在后台运行,您会收到推送通知。 如果它在前台运行,则会触发didReceiveLocalNotification。 点击通知将启动您的应用程序并将其带到前台,并同时触发didReceiveLocalNotification


0

注意:AppCoda有一个非常棒的教程:https://www.appcoda.com/push-notification-ios/ - Pranav Wadhwa
我实际上正在寻找推送通知而不是普通通知,但还是谢谢。 - SwiftyJD

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接