Skip to content

Commit

Permalink
1、通知を送信するメソッドをlib/assets以下に切り出した
Browse files Browse the repository at this point in the history
2、応援されたときにユーザーに通知を送信する
  • Loading branch information
Ryoma-Suizu committed Dec 14, 2018
1 parent 38cbd7a commit dca38aa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
9 changes: 8 additions & 1 deletion app/graphql/mutations/create_clap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ class Mutations::CreateClap < GraphQL::Schema::Mutation

def resolve(task_id:)
task = Task.find(task_id)
task.clap = task.clap + 1
count = task.clap + 1
task.clap = count
if task.save
{ task: task, errors: [] }
else
{ task: task, errors: task.errors.full_messages }
end

client = User.find(task.user_id)
body = {
name: "あなたの頑張りを応援しています! 応援人数:#{count}人"
}
notification(client, body)
end
end
25 changes: 25 additions & 0 deletions lib/assets/notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#####################################################################################
#####################################################################################
#第一引数のclientにはcurrent_user.idとかでユーザーモデルを検索した結果のActiveRecordsを
#第二引数のbodyには{name: "hoge"}みたいな形でjsonっぽく書いた文字列を渡してください
#####################################################################################
#####################################################################################

def notification(client, body)
payload = {
endpoint: client.endpoint, # ブラウザでregistration.pushManager.getSubscription()で取得したsubscriptionのendpoint
p256dh: client.key, # 同じくsubscriptionのp256dh
auth: client.auth, # 同じくsubscriptionのauth
ttl: 86400, # 任意の値
vapid: {
subject: 'https://task-driver.sukiyaki.party', # APPサーバのコンタクト用URIとか('mailto:' もしくは 'https://')
public_key: ENV['VAPID_PUBLIC_KEY'],
private_key: ENV['VAPID_SECRET_KEY']
},
message: {
title: "task",
body: body
}.to_json
}
Webpush.payload_send(payload) #送信
end
22 changes: 1 addition & 21 deletions lib/tasks/push.rake
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
def notification(client, body)
payload = {
endpoint: client.endpoint, # ブラウザでregistration.pushManager.getSubscription()で取得したsubscriptionのendpoint
p256dh: client.key, # 同じくsubscriptionのp256dh
auth: client.auth, # 同じくsubscriptionのauth
ttl: 86400, # 任意の値
vapid: {
subject: 'https://task-driver.sukiyaki.party', # APPサーバのコンタクト用URIとか('mailto:' もしくは 'https://')
public_key: ENV['VAPID_PUBLIC_KEY'],
private_key: ENV['VAPID_SECRET_KEY']
},
message: {
title: "task",
body: body
}.to_json
}
Webpush.payload_send(payload) #送信
end


task :push_notification => :environment do
hour = DateTime.now.hour
#if(!(hour > 1 && hour < 7)) then
Expand All @@ -33,7 +13,7 @@ task :push_notification => :environment do
res = JSON.parse(http.get("/tasks/importance?id=#{client.id}")) #resには配列がはいる
}
body = {
name: res[0].name + "最優先タスクとして残ってるよ!",
name: res[0].name + "が最優先タスクとして残ってるよ!",
id: res[0].id,
target_url: "/tasks/"

Expand Down

0 comments on commit dca38aa

Please sign in to comment.