プッシュ通知は従来のマーケティング手法で言えばメールマーケティングに相当します。つまり一度配信してしまうと、後から取り消すのが難しい拡散方法ということです。そのため企業によっては一旦承認フローを経てからプッシュ通知を送りたいという要望も聞かれます。
そこで独自のプッシュ通知管理画面を作成して、承認フローを経るようにしてみたいと思います。作成していく過程をステップを踏んで説明していきますので、自社のフローに合わせてカスタマイズしてみてください。
今回はプッシュ通知の仮保存処理などを作っていきます。
保存処理
下書き保存処理を行えるようにします。
保存しておくデータはすべて data.message
の中に入れておきます。そして、仮保存なので prePush
というデータストアのクラスに保存するように指定します。
デフォルトでは data.objectId
をnullで持っているので、その値はデータストアにセットしないようにします。また、その値のあるなしによって save
または update
を実行するようにします。
saveDraft: function() { let prePush = new (this.ncmb.DataStore('prePush')); let me = this; for (let key in this.message) { if (key === 'objectId' && !this.message.objectId) { continue; } prePush.set(key, this.message[key]); } let method = me.message.objectId ? 'update' : 'save'; prePush[method]() .then(function(obj) { me.message.objectId = obj.objectId; me.$emit('successMessage', "保存しました"); }) },
配信予定数確認
プッシュ通知の配信予定数を確認する処理は、実際にInstallationを検索してみます。
まず配信対象を選択した時点で処理を行うようにします。
updateTarget: function() { this.message.targets = []; if (this.message.forAndroid) this.message.targets.push('android'); if (this.message.forIos) this.message.targets.push('ios'); this.countTarget(); },
次に配信端末のドロップダウンによって処理分けをします。セグメンテーションから選択しているのか、検索条件を設定しているかの違いがあります。
countTarget: function() { if (this.message.targets.length === 0) { return true; } switch (this.message.segmentationSelected) { case '1': this.countTargetByConditions(this.message.searchConditions); break; case '0': this.searchInstallation(); break; default: let segmentation = this.segmentations.filter((segmentation) => { if (segmentation.objectId === this.message.segmentationSelected) return segmentation; })[0]; if (!segmentation) return; this.countTargetByConditions(segmentation.segmentations); } },
いずれの場合も検索条件を組み立てながらInstallationを絞り込んでいきます。
countTargetByConditions: function(searchConditions) { let installation = this.ncmb.Installation; for (let i = 0; i < searchConditions.length; i++) { let searchCondition = searchConditions[i]; if (typeof searchCondition.decision === 'undefined' || typeof searchCondition.target === 'undefined' || typeof searchCondition.targetValue === 'undefined') { continue; } switch (searchCondition.type) { case "number": searchCondition.targetValue = parseInt(searchCondition.targetValue); break; } installation = installation[searchCondition.decision](searchCondition.target, searchCondition.targetValue); } this.searchInstallation(installation); },
カラムの型によって値を変更する必要があるでしょう。
最後に実際に検索をします。この時、 count
を使って結果行数を取得します。
searchInstallation: function(installation) { if (!installation) installation = this.ncmb.Installation; installation .in('deviceType', this.message.targets) .count() .fetchAll() .then((result) => { this.installation_count = result.count; }) }
ここまでの操作によって検索条件の設定によって配信対象端末数が分かるようになります。
今回のコードはNCMBMania/pushManagerにアップロードされています。実装方法などの参考にしてください。
プッシュ通知を複数人の目を通さなければ送信できないようにすることでミスを大幅に軽減できます。参考にしてください。