Skip to content

Commit ae79eb5

Browse files
authored
Merge pull request #2 from tigi44/feature/update
Feature/update
2 parents 3fc007e + 66ea25c commit ae79eb5

33 files changed

+2415
-979
lines changed

.gitignore

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Logs
2+
logs
3+
*.log
4+
15
# Runtime data
26
pids
37
*.pid
@@ -26,6 +30,7 @@ node_modules
2630
npm-debug.log
2731

2832
# apns .pem
29-
apns/keys/cert.pem
30-
apns/keys/key.pem
31-
apns/keys/*
33+
apns/keys/cert/*
34+
apns/keys/key/*
35+
!apns/keys/cert/test_cert.pem
36+
!apns/keys/key/test_key.pem

Class/DataLayer/APNsDataSource.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const apn = require('apn')
2+
const Logger = require('../../Modules/Logger')
3+
4+
const sandboxGateway = 'gateway.sandbox.push.apple.com';
5+
const gateway = 'gateway.push.apple.com';
6+
7+
class APNsDataSource {
8+
9+
options = {
10+
// token: {
11+
// key: "path/to/APNsAuthKey_XXXXXXXXXX.p8",
12+
// keyId: "key-id",
13+
// teamId: "developer-team-id"
14+
// },
15+
// proxy: {
16+
// host: "192.168.10.92",
17+
// port: 8080
18+
// },
19+
20+
cert: './apns/keys/cert/cert.pem',
21+
key: './apns/keys/ket/key.pem',
22+
production: false
23+
}
24+
25+
sendPush(certPemFilePath, keyPemFilePath, isProduction, appId, token, jsonData) {
26+
console.log(certPemFilePath)
27+
console.log(keyPemFilePath)
28+
console.log(isProduction)
29+
console.log(appId)
30+
console.log(token)
31+
console.log(jsonData)
32+
if (!certPemFilePath || !keyPemFilePath || !appId || !token || jsonData == null || jsonData.aps == null) {
33+
return Promise.reject(new Error('Error : send push parameter'));
34+
}
35+
36+
var options = this.options;
37+
38+
options.production = isProduction ? true : false;
39+
options.cert = certPemFilePath
40+
options.key = keyPemFilePath
41+
42+
var apnProvider = new apn.Provider(options);
43+
var notification = new apn.Notification();
44+
45+
notification.badge = jsonData.aps.badge;
46+
notification.sound = jsonData.aps.sound;
47+
notification.alert = jsonData.aps.alert;
48+
notification.threadId = jsonData.aps["thread-id"];
49+
notification.category = jsonData.aps["category"];
50+
51+
delete jsonData.aps;
52+
notification.payload = jsonData;
53+
notification.topic = appId;
54+
55+
Logger.debug("=== Start APNs Push ====");
56+
Logger.debug("gateway : " + (options.production ? gateway : sandboxGateway));
57+
Logger.debug("production : " + options.production);
58+
Logger.debug("appId : " + appId);
59+
Logger.debug("token : " + token);
60+
Logger.debug("notification : " + JSON.stringify(notification));
61+
62+
return apnProvider.send(notification, token).then( (result) => {
63+
if (result.failed.length > 0) {
64+
Logger.debug("=== Error Fail to send push : " + JSON.stringify(result));
65+
throw new Error(JSON.stringify(result.failed));
66+
}
67+
Logger.debug("=== End APNs Push ====");
68+
});
69+
}
70+
}
71+
72+
module.exports = APNsDataSource;

Class/DataLayer/APNsRepository.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const APNsRepositoryInterface = require('../DomainLayer/RepositoryInterface/APNsRepositoryInterface')
2+
3+
class APNsRepository extends APNsRepositoryInterface {
4+
5+
sendPush(certPemFilePath, keyPemFilePath, isProduction, appId, token, jsonData) {
6+
return this.dataSource.sendPush(certPemFilePath, keyPemFilePath, isProduction, appId, token, jsonData)
7+
}
8+
}
9+
10+
module.exports = APNsRepository;

Class/DataLayer/FileDataSource.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
class FileDataSource {
5+
findPemFile(dirPath) {
6+
7+
if (!fs.existsSync(dirPath)){
8+
fs.mkdirSync(dirPath, { recursive: true })
9+
}
10+
11+
var pemFileName
12+
let files = fs.readdirSync(dirPath);
13+
14+
files.sort(function(a, b) {
15+
return fs.statSync(dirPath + b).mtime.getTime() - fs.statSync(dirPath + a).mtime.getTime();
16+
});
17+
18+
for (var file of files) {
19+
if (path.extname(file) === '.pem') {
20+
pemFileName = file
21+
break
22+
}
23+
}
24+
25+
return pemFileName
26+
}
27+
}
28+
29+
module.exports = FileDataSource;

Class/DataLayer/FileRepository.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const FileRepositoryInterface = require('../DomainLayer/RepositoryInterface/FileRepositoryInterface')
2+
3+
class FileRepository extends FileRepositoryInterface {
4+
5+
findPemFile(dirPath) {
6+
return Promise.resolve(this.dataSource.findPemFile(dirPath))
7+
}
8+
}
9+
10+
module.exports = FileRepository;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const UseCase = require('./UseCase')
2+
3+
class GetPemFilePathUseCase extends UseCase {
4+
5+
execute(dirPath) {
6+
7+
return this.repository.findPemFile(dirPath).then(pemFile => {
8+
return dirPath + pemFile
9+
})
10+
}
11+
}
12+
13+
module.exports = GetPemFilePathUseCase
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class APNsRepositoryInterface {
2+
3+
constructor(dataSource) {
4+
this.dataSource = dataSource;
5+
}
6+
7+
sendPush(certPemFilePath, keyPemFilePath, isProduction, appId, token, jsonData) {}
8+
}
9+
10+
module.exports = APNsRepositoryInterface;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class FileRepositoryInterface {
2+
3+
constructor(dataSource) {
4+
this.dataSource = dataSource;
5+
}
6+
7+
findPemFile(dirPath) {}
8+
}
9+
10+
module.exports = FileRepositoryInterface;

Class/DomainLayer/SendPushUseCase.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const UseCase = require('./UseCase')
2+
3+
class SendPushUseCase extends UseCase {
4+
5+
execute(certPemFilePath, keyPemFilePath, isProduction, appId, token, jsonData) {
6+
7+
return this.repository.sendPush(certPemFilePath, keyPemFilePath, isProduction, appId, token, jsonData)
8+
}
9+
}
10+
11+
module.exports = SendPushUseCase

Class/DomainLayer/UseCase.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class UseCase {
2+
3+
constructor(repository) {
4+
this.repository = repository;
5+
}
6+
7+
execute() {}
8+
}
9+
10+
module.exports = UseCase;

0 commit comments

Comments
 (0)