File tree Expand file tree Collapse file tree 2 files changed +29
-5
lines changed Expand file tree Collapse file tree 2 files changed +29
-5
lines changed Original file line number Diff line number Diff line change 65
65
->send();
66
66
```
67
67
68
+ If You want to send a FCM to topic, use method toTopic($topic) instead to() :
69
+ ``` php
70
+ fcm()
71
+ ->toTopic($topic) // $topic must an string (topic name)
72
+ ->notification([
73
+ 'title' => 'Test FCM',
74
+ 'body' => 'This is a test of FCM',
75
+ ])
76
+ ->send();
77
+ ```
78
+
68
79
If You want to send a FCM with just notification parameter,this is an example of usage sending a FCM with only notification parameter :
69
80
``` php
70
81
fcm()
Original file line number Diff line number Diff line change 9
9
class Fcm
10
10
{
11
11
protected $ recipient ;
12
+ protected $ topic ;
12
13
protected $ data ;
13
14
protected $ notification ;
14
15
@@ -19,6 +20,13 @@ public function to(array $recipient)
19
20
return $ this ;
20
21
}
21
22
23
+ public function toTopic (string $ topic )
24
+ {
25
+ $ this ->topic = $ topic ;
26
+
27
+ return $ this ;
28
+ }
29
+
22
30
public function data (array $ data = [])
23
31
{
24
32
$ this ->data = $ data ;
@@ -38,17 +46,22 @@ public function send()
38
46
$ fcmEndpoint = 'https://fcm.googleapis.com/fcm/send ' ;
39
47
40
48
$ fields = [
41
- 'registration_ids ' => $ this ->recipient ,
42
49
'content-available ' => true ,
43
50
'priority ' => 'high ' ,
44
51
'data ' => $ this ->data ,
45
52
'notification ' => $ this ->notification
46
53
];
47
54
55
+ if ($ this ->topic ) {
56
+ $ fields ['to ' ] = "/topics/ " . $ this ->topic ;
57
+ } else {
58
+ $ fields ['registration_ids ' ] = $ this ->recipient ;
59
+ }
60
+
48
61
$ serverKey = config ('laravel-fcm.server_key ' );
49
62
50
63
$ headers = [
51
- 'Authorization:key= ' . $ serverKey ,
64
+ 'Authorization:key= ' . $ serverKey ,
52
65
'Content-Type:application/json '
53
66
];
54
67
@@ -60,8 +73,8 @@ public function send()
60
73
curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , CURL_IPRESOLVE_V4 );
61
74
curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ fields ));
62
75
$ result = json_decode (curl_exec ($ ch ));
63
- curl_close ($ ch );
64
-
65
- return $ result ;
76
+ curl_close ($ ch );
77
+
78
+ return $ result ;
66
79
}
67
80
}
You can’t perform that action at this time.
0 commit comments