【问题标题】:How to send a push notification after a button is tapped? [closed]点击按钮后如何发送推送通知? [关闭]
【发布时间】:2016-07-14 20:38:31
【问题描述】:

我目前有一个已设置为接收推送通知的 iOS 应用程序。我想要做的是,在用户按下“加入”按钮后,我想向他们发送“欢迎”推送通知。关于如何做到这一点的任何线索?

【问题讨论】:

    标签: ios swift push-notification


    【解决方案1】:

    这很容易。

    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

    【讨论】:

      【解决方案2】:

      在 YouTube 上,Jared Davidson 提供了一些很棒的 iOS 教程。 他有两个通知:

      这正是你所需要的:

      https://www.youtube.com/watch?v=tqJFJzUPpcI

      ...还有一个用于远程通知(不带按钮)

      https://www.youtube.com/watch?v=LBw5tuTvKd4

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 1970-01-01
      • 2019-06-11
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多