개발/안드로이드
[안드로이드/android] FCM 푸시알람 구현
공상과학소설
2019. 7. 22. 11:59
반응형
나는 알람 기능을 구현하기 위해 FCM을 적용하기로 했다.
Firebase 클라우드 메시징(FCM)은 무료로 메시지를 안정적으로 전송할 수 있는 교차 플랫폼 메시징 설루션이다.
내가 이 기능을 넣고 싶은 것은 친구 추가나 게시물이 올라오면 모든 유저들에게 푸시 알람을 주고 싶어서였다.
push 함수
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
private void Push(String Name) {
class pushmessage extends AsyncTask<String, Void, String> {
String target;
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
try {
String Name = (String) params[0];
URL url = new URL(target);//URL 객체 생성
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null) {
break;
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(final String result) {
}
}
pushmessage task = new pushmessage();
task.execute(Name);
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
on_loginAlarm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
<?php
define(GOOGLE_API_KEY,"내 API");
$Name = $_POST["Name"];
function send_notification ($tokens, $jsonMessage)
{
$headers = array(
'Authorization:key =' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($jsonMessage));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
// 발송 대상자 디비에 저장한 token 을 가져와 배열에 담는다.
$tokens = array();
$tokens[] = "";
// 발송 메시지
$jsonMessage = array(
"to" =>"내 기기 ",
"priority"=>"high",
"notification" => array("title"=>"'$Name' 님이 로그인하셨습니다",
"body"=>"눌러서 확인해보세요!"),
"data"=>array("title"=>"'$Name' 님이 로그인하셨습니다",
"message"=>"눌러서 확인해보세요!")
);
// 발송
$message_status = send_notification($tokens, $jsonMessage);
echo $message_status;
?>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
지금은 내가 등록한 한대의 기기에서만 푸시 알림이 뜨는데 좀 더 알아보고 모든 유저들에게 보낼 수 있는 방법을 찾아내겠다.
반응형