こんにちは。
現在覚えたての知識で、テーブルに並んだリストの値をランダムでプッシュ通知するアプリを作っているのですが、現状だと同じcellばかりプッシュされてしまうのでどこで値を変えればいいか、というところで詰まっています。
関連性のあるクラスファイルのみ以下記載します
.
.
.
テーブルビュー
swift
1 2import UIKit 3 4var list = ["東京", "千葉", "埼玉"] 5var notiList = list 6 7var randomItem = Int(arc4random_uniform(UInt32(list.count))) 8 9 10class ListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 11 12 // Outlets 13 @IBOutlet weak var listTableView: UITableView! 14 @IBOutlet weak var scrollView: UIScrollView! 15 16 // variables 17 var listScrollView = UIScrollView() 18 19 20 21 22 23 // delegate method to assign the number of cell 24 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 25 26 return list.count 27 } 28 29 30 31 // dataSource method to set the value to cell 32 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 33 34 let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell") 35 cell.textLabel?.text = list[indexPath.row] 36 37 return cell 38 } 39 40 41 42 // have to add this function because it has 2 buttons 43 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 44 return true 45 } 46 47 48 49 // swipe actions edit/delete 50 func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 51 52 // delete action 53 let deleteButton = UITableViewRowAction(style: .destructive, title: "Delete") { (rowAction, indexPath) in 54 list.remove(at: indexPath.row) 55 self.listTableView.reloadData() 56 57 } 58 deleteButton.backgroundColor = UIColor.red 59 60 return [deleteButton] 61 } 62 63 64 65 // display change to setting vc 66 @IBAction func editButton(_ sender: Any) { 67 68 let storyboard: UIStoryboard = self.storyboard! 69 let settingVC = storyboard.instantiateViewController(withIdentifier: "SettingVC") 70 self.present(settingVC, animated: true, completion: nil) 71 72 } 73 74 75 override func viewDidLoad() { 76 super.viewDidLoad() 77 LocalPushManager.shared.requestAuthorization() 78 79 } 80 81 82} 83
.
.
.
notification操作
swift
1 2import Foundation 3import UserNotifications 4 5class LocalPushManager: NSObject { 6 7 // instance 8 static var shared = LocalPushManager() 9 let center = UNUserNotificationCenter.current() 10 11 12 // this is the authorisation to access the notification center 13 func requestAuthorization(){ 14 center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in 15 if error == nil{ 16 print("parmission granted") 17 } 18 } 19 } 20 21 22 23 func sendLocalPush(in time: TimeInterval){ 24 25 let content = UNMutableNotificationContent() 26 content.body = NSString.localizedUserNotificationString(forKey: list[randomItem], arguments: nil) 27 28 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: time, repeats: true) 29 let request = UNNotificationRequest(identifier: "Timer", content: content, trigger: trigger) 30 31 center.add(request) { (error) in 32 if error == nil{ 33 print("schedule push succeed") 34 } 35 } 36 37 } 38 39} 40
.
.
.
タイマー設定
swift
1import Foundation 2import UIKit 3 4class SettingViewController: UIViewController { 5 6 @IBOutlet weak var timeField: UITextField! 7 8 9 10 @IBAction func setButton(_ sender: Any) { 11 12 guard let text = timeField.text else {return} 13 guard let time = TimeInterval(text) else {return} 14 LocalPushManager.shared.sendLocalPush(in: time) 15 16 self.dismiss(animated: true, completion: nil) 17 18 } 19 20 21} 22 23
.
.
#アイデア?
ランダムに要素を取得する randItem がグローバルなので notificationContent の forKey に変数をこのまま与えてしまうと、グローバルで作成されたランダム数がずっと入り続けてしまうのが原因なのはわかりました。
なので、cellの情報が詰まったlistをプッシュのディスプレイ用に複製(var notiList = list)して、
以下のようなのを用いて使ったら削除して、別のを取得してそれを代入し直し、というプロセスとどこかに入れたらいいのでは?と思いついたのですが、、、
この func sendLocalPushは一度呼ばれるだけなので、ここで毎回発火させるのは難しい。。。?
swift
1notiList.remove(at: randomItem) 2randomItem = Int(arc4random_uniform(UInt32(notiList.count))) 3 4content.body = NSString.localizedUserNotificationString(forKey: notiList[randomItem], arguments: nil)
ほかに何か解決策があればよろしくお願いいたします。