0
私は、認定されたユーザーに、最初にアプリを停止するときに、オンラインアンケートへのリンクを送る機能を実装する必要があります。理想的には、私はいくつかのタイプの通知(ローカル、プッシュなど)でこれを行います。アプリを最初に再起動してサーベイリンクを開くなどの一時停止のときに、アプリに通知をトリガーする方法はありますか?iOS:ユーザーにアプリを中断したときに通知または警告するにはどうすればよいですか?
私は、認定されたユーザーに、最初にアプリを停止するときに、オンラインアンケートへのリンクを送る機能を実装する必要があります。理想的には、私はいくつかのタイプの通知(ローカル、プッシュなど)でこれを行います。アプリを最初に再起動してサーベイリンクを開くなどの一時停止のときに、アプリに通知をトリガーする方法はありますか?iOS:ユーザーにアプリを中断したときに通知または警告するにはどうすればよいですか?
AppDelegate
には、以前にアプリを開いたことがあるかどうかを保存する必要があります。最後に
AppDelegate
//make sure to import the framework
//additionally, if you want to customize the notification's UI,
//import the UserNotificationsUI
import UserNotifications
//default value is true, because it will be set false if this is not the first launch
var firstLaunch: Bool = true
let defaults = UserDefaults.standard
//also make sure to include *UNUserNotificationCenterDelegate*
//in your class declaration of the AppDelegate
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
//get whether this is the very first launch
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let bool = defaults.object(forKey: "firstLaunch") as? Bool {
firstLaunch = bool
}
defaults.set(false, forKey: "firstLaunch")
defaults.synchronize()
//ask the user to allow notifications
//maybe do this some other place, where it is more appropriate
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in}
return true
}
//schedule your notification when exiting the app, if necessary
func applicationDidEnterBackground(_ application: UIApplication) {
//update the variable
if let bool = defaults.object(forKey: "firstLaunch") as? Bool {
firstLaunch = bool
}
if !firstLaunch {
//abort mission if it's not the first launch
return
}
//customize your notification's content
let content = UNMutableNotificationContent()
content.title = "Survey?"
content.body = "Would you like to take a quick survey?"
content.sound = UNNotificationSound.default()
//schedule the notification
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: "takeSurvey", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: nil)
}
、あなたが得た応答を処理し、あなたのリンクを開きます。それでおしまい!
"didFinishLaunching"でfirstLaunch = falseを設定しているので... "didEnterBackground()"ではどのようにこれが本当ですか?または私は何かを逃していますか? – OliverM
それは本当です。あなたは私のコードを試しましたか?私のために、それは自動的に機能します。もしそうでなければ、通知がスケジュールされた直後や 'firstLaunch'が読み込まれた直後に' applicationDidEnterBackground() 'にその2行を移動することができます。私はそれがどのように行くのか教えてください! – LinusGeffarth