'#fcm'에 해당되는 글 1건

  1. 2019.02.19 classic ASP FCM 서버 예제
2019. 2. 19. 16:15
<%@ language="VBScript" %>

<%
Response.charset = "utf-8"
Response.ContentType="text/html"
%>

<%
Dim DBCon
Set DBCon = Server.CreateObject("ADODB.Connection")
DBCon.Open "Provider=SQLOLEDB.1;Password=****;Persist Security Info=True;User ID=****;Initial Catalog=****;Data Source=***.***.***.***"
DBCon.Execute("Set Transaction Isolation Level Read Uncommitted")
Server.ScriptTimeOut=300

Dim randKey : randKey = ""
Dim id : id = request("id") '메시지를 받을 userID (DB에서 사용자 검색용)
Dim title : title = Request("title") 'push 메시지 타이틀 (메시지 제목
Dim message : message = Request("message") 'push 메시지 내용
Dim push_service_id : push_service_id = ""
Dim content_available : content_available = "true"
Dim priority : priority = "height"

'안드 푸시요청 URL
PushServerURL = "https://gcm-http.googleapis.com/gcm/send"

'서버용APIKEY
ApplicationAPIKey = "AAAAHLcAjEA:APA91bE****....."

'DB에 저장된 사용자 api를 불러옵니다
Sql = "Select * From devices Where user_id = 1"

Set RS = SERVER.CreateObject("ADODB.Recordset"
RS.CURSORLOCATION = 3
RS.CURSORTYPE = 3
RS.LOCKTYPE = 3     
RS.Open Sql, DBCon

If RS.RecordCount > 0 then  
push_service_id = RS("push_service_id")
End If

RS.close

token = push_service_id
token = """"&token&""""
title = "push messsage title"
message = "push message body"

Set RS = nothing   

'// JSON 조합
'// 문법이 틀릴경우 안됨 (400 에러 납니다)
'// 2명 이상일 경우 "to" => "registration_ids" : ['fdsafd', 'fdasfdsa' ... ] 형식으로 하면 됩니다(한번에 최대 1,000명까지 송신됨)
'// https://firebase.google.com/docs/cloud-messaging/http-server-ref?hl=ko 여기서 메시지 형식 참조
postJSONData = "" & _
"{" & _
" ""to"" : " & token & " " & _
", ""notification"": {" & _
" ""title"" : """ & title & """" & _
" , ""body"" : """ & message & """" & _
" , ""icon"" : """ & "ic_launcher"& """" & _
" , ""color"" : """ & "#rrggbb" & """" & _
" }" & _
" , ""data"": {" & _
" ""title"" : """ & title & """" & _
" , ""message"" : """ & message & """" & _
" , ""randKey"" : """ & "ddddd" & """" & _
" }" & _
"}"

'// 전송
Response.write postJSONData
Set httpObj = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
httpObj.open "POST" , PushServerURL, False
httpObj.SetRequestHeader "Content-Type", "application/json"
httpObj.SetRequestHeader "Authorization", "key=" & ApplicationAPIKey
httpObj.Send postJSONData
httpObj.WaitForResponse

If httpObj.Status = "200" Then
'Response.Write("전송성공 : " & httpObj.ResponseText)
Response.write "{"&Chr(34)&"result"&Chr(34)&":"&Chr(34)&"surceess"&Chr(34)&"}"

Else
'Response.Write("전송실패 : " & httpObj.ResponseText)
Response.write "{"&Chr(34)&"result"&Chr(34)&":"&Chr(34)&"false"&Chr(34)&"}" & httpObj.Status
End If
%>


Posted by 토실토실천재