Skip to content

Commit 419ff9a

Browse files
committed
Add toolkit for fcm/apns push testing.
1 parent 9b4d23e commit 419ff9a

File tree

7 files changed

+705
-0
lines changed

7 files changed

+705
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@
1616
"has_video": false,
1717
}
1818
```
19+
20+
## push test tool
21+
22+
Please refer to the [Tool](/tools/) to test callkeep offline push.

tools/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# CallKeep test tool
2+
3+
This tool is used to demonstrate the basic push process for verifying callkeep. The tool is written in golang.
4+
5+
## For iOS APNS
6+
7+
please refer to `https://developer.apple.com/account/resources/certificates/add`.
8+
Choose `VoIP Services Certificate` to create a push certificate, download voip_services.cer and install it to the keychain tool, and export its private key rename it to `callkeep-apns.p12`
9+
10+
`go run cmd/main.go -i +8618612345678 -p apns -d $ios_device_token`
11+
12+
## For Android FCM
13+
14+
please refer to `https://console.firebase.google.com/project/[your project]/settings/serviceaccounts/adminsdk`
15+
Select the `go` sdk format under your fcm project to download serviceAccountKey.json and rename it to `callkee-fcm.json`
16+
17+
`go run cmd/main.go -i +8618612345678 -p fcm -d $android_fcm_token`

tools/cmd/main.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"flag"
6+
"fmt"
7+
8+
"github.com/flutter-webrtc/callkeep/tools/pkg/fcm"
9+
"github.com/flutter-webrtc/callkeep/tools/pkg/pushkit"
10+
log "github.com/pion/ion-log"
11+
)
12+
13+
func pushCallKeep(provider string, token string, payload map[string]string) error {
14+
log.Infof("CallKeep Push Request")
15+
16+
log.Infof("provider=%v", provider)
17+
log.Infof("token=%v", token)
18+
19+
data, _ := json.MarshalIndent(payload, "", " ")
20+
log.Infof("payload=\n%v", string(data))
21+
22+
switch provider {
23+
case "apns":
24+
pushkit.APNSPush("./callkeep-apns.p12", token, payload)
25+
return nil
26+
case "fcm":
27+
fcm.FCMPush("./callkeep-fcm.json", token, payload)
28+
return nil
29+
}
30+
return fmt.Errorf("%v provider not found", provider)
31+
}
32+
33+
func main() {
34+
log.Init("info")
35+
h := false
36+
flag.BoolVar(&h, "h", false, "help")
37+
provider := ""
38+
flag.StringVar(&provider, "p", "fcm", "push provider type fcm | apns.")
39+
token := ""
40+
flag.StringVar(&token, "d", "", "device token.")
41+
cid := ""
42+
flag.StringVar(&cid, "i", "", "caller id.")
43+
flag.Parse()
44+
45+
if h || len(token) == 0 || len(cid) == 0 {
46+
flag.Usage()
47+
return
48+
}
49+
50+
log.Infof("try push")
51+
52+
payload := map[string]string{
53+
"caller_id": cid,
54+
"caller_name": "push test",
55+
"caller_id_type": "number",
56+
"has_video": "false",
57+
}
58+
59+
err := pushCallKeep(provider, token, payload)
60+
if err != nil {
61+
log.Errorf("push failed %v", err)
62+
}
63+
}

tools/go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/flutter-webrtc/callkeep/tools
2+
3+
go 1.16
4+
5+
require (
6+
cloud.google.com/go/firestore v1.5.0 // indirect
7+
firebase.google.com/go v3.13.0+incompatible
8+
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
9+
github.com/pion/ion-log v1.2.0
10+
github.com/sideshow/apns2 v0.20.0
11+
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
12+
google.golang.org/api v0.48.0
13+
)

tools/go.sum

Lines changed: 537 additions & 0 deletions
Large diffs are not rendered by default.

tools/pkg/fcm/fcm.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package fcm
2+
3+
import (
4+
"context"
5+
6+
firebase "firebase.google.com/go"
7+
"firebase.google.com/go/messaging"
8+
log "github.com/pion/ion-log"
9+
"google.golang.org/api/option"
10+
)
11+
12+
func FCMPush(fcmCert string, token string, payload map[string]string) (string, error) {
13+
opt := option.WithCredentialsFile(fcmCert)
14+
app, err := firebase.NewApp(context.Background(), nil, opt)
15+
16+
// Obtain a messaging.Client from the App.
17+
ctx := context.Background()
18+
client, err := app.Messaging(ctx)
19+
20+
// See documentation on defining a message payload.
21+
message := &messaging.Message{
22+
Data: payload,
23+
Token: token,
24+
}
25+
26+
// Send a message to the device corresponding to the provided
27+
// registration token.
28+
response, err := client.Send(ctx, message)
29+
if err != nil {
30+
log.Errorf("FCMPush: err %v", err)
31+
return "", err
32+
}
33+
// Response is a message ID string.
34+
log.Infof("Successfully sent message: %v", response)
35+
return response, err
36+
}

tools/pkg/pushkit/pushkit.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package pushkit
2+
3+
import (
4+
"encoding/json"
5+
6+
log "github.com/pion/ion-log"
7+
"github.com/sideshow/apns2"
8+
"github.com/sideshow/apns2/certificate"
9+
)
10+
11+
func APNSPush(p12 string, token string, payload map[string]string) {
12+
13+
cert, err := certificate.FromP12File(p12, "")
14+
if err != nil {
15+
log.Errorf("Cert Error: %v", err)
16+
}
17+
18+
data, _ := json.Marshal(payload)
19+
notification := &apns2.Notification{}
20+
notification.DeviceToken = token
21+
notification.PushType = "voip" // voip | alert
22+
notification.Payload = []byte(data) // See Payload section below
23+
24+
// If you want to test push notifications for builds running directly from XCode (Development), use
25+
client := apns2.NewClient(cert).Development()
26+
// For apps published to the app store or installed as an ad-hoc distribution use Production()
27+
//client := apns2.NewClient(cert).Production()
28+
res, err := client.Push(notification)
29+
30+
if err != nil {
31+
log.Errorf("Error: %v", err)
32+
}
33+
34+
log.Infof("Push response: %v %v %v\n", res.StatusCode, res.ApnsID, res.Reason)
35+
}

0 commit comments

Comments
 (0)