diff --git a/README.md b/README.md index 0c6bfca..cc47ab4 100644 --- a/README.md +++ b/README.md @@ -8,3 +8,18 @@ Status](https://travis-ci.org/parse-server-modules/parse-server-push-adapter.svg Official Push adapter for parse-server See [parse-server push configuration](https://github.com/ParsePlatform/parse-server/wiki/Push) + + +### see more logs + +You can enable verbose logging with environment variables: + +``` +VERBOSE=1 + +or + +VERBOSE_PARSE_SERVER_PUSH_ADAPTER=1 +``` + +This will produce a more verbose output for all the push sending attempts diff --git a/package.json b/package.json index e3c3a34..9890dc4 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ ], "scripts": { "build": "./node_modules/.bin/babel src/ -d lib/", - "test": "TESTING=1 babel-node ./node_modules/.bin/isparta cover -x **/spec/** ./node_modules/jasmine/bin/jasmine.js", + "test": "TESTING=1 babel-node ./node_modules/.bin/isparta cover --root src/ ./node_modules/jasmine/bin/jasmine.js", "prepublish": "npm run build" }, "keywords": [ @@ -34,6 +34,7 @@ "dependencies": { "apn": "^1.7.5", "node-gcm": "^0.14.0", + "npmlog": "^2.0.3", "parse": "^1.8.1" } } diff --git a/src/APNS.js b/src/APNS.js index 398add6..16d8aae 100644 --- a/src/APNS.js +++ b/src/APNS.js @@ -1,9 +1,12 @@ "use strict"; -const Parse = require('parse/node').Parse; // TODO: apn does not support the new HTTP/2 protocal. It is fine to use it in V1, // but probably we will replace it in the future. -const apn = require('apn'); +import apn from 'apn'; +import Parse from 'parse'; +import log from 'npmlog'; + +const LOG_PREFIX = 'parse-server-push-adapter APNS'; /** * Create a new connection to the APN service. @@ -46,7 +49,7 @@ function APNS(args) { // Set apns client callbacks conn.on('connected', () => { - console.log('APNS Connection %d Connected', conn.index); + log.verbose(LOG_PREFIX, 'APNS Connection %d Connected', conn.index); }); conn.on('transmissionError', (errCode, notification, apnDevice) => { @@ -54,15 +57,15 @@ function APNS(args) { }); conn.on('timeout', () => { - console.log('APNS Connection %d Timeout', conn.index); + log.verbose(LOG_PREFIX, 'APNS Connection %d Timeout', conn.index); }); conn.on('disconnected', () => { - console.log('APNS Connection %d Disconnected', conn.index); + log.verbose(LOG_PREFIX, 'APNS Connection %d Disconnected', conn.index); }); conn.on('socketError', () => { - console.log('APNS Connection %d Socket Error', conn.index); + log.verbose(LOG_PREFIX, 'APNS Connection %d Socket Error', conn.index); }); conn.on('transmitted', function(notification, device) { @@ -76,7 +79,7 @@ function APNS(args) { } }); } - console.log('APNS Connection %d Notification transmitted to %s', conn.index, device.token.toString('hex')); + log.verbose(LOG_PREFIX, 'APNS Connection %d Notification transmitted to %s', conn.index, device.token.toString('hex')); }); this.conns.push(conn); @@ -106,6 +109,7 @@ APNS.prototype.send = function(data, devices) { let qualifiedConnIndexs = chooseConns(this.conns, device); // We can not find a valid conn, just ignore this device if (qualifiedConnIndexs.length == 0) { + log.error(LOG_PREFIX, 'no qualified connections for %s %s', device.appIdentifier, device.deviceToken); return Promise.resolve({ transmitted: false, device: { @@ -154,6 +158,7 @@ function handleTransmissionError(conns, errCode, notification, apnDevice) { // There is no more available conns, we give up in this case if (newConnIndex < 0 || newConnIndex >= conns.length) { if (apnDevice.callback) { + log.error(LOG_PREFIX, `cannot find vaild connection for ${apnDevice.token.toString('hex')}`); apnDevice.callback({ response: {error: `APNS can not find vaild connection for ${apnDevice.token.toString('hex')}`, code: errCode}, status: errCode, diff --git a/src/GCM.js b/src/GCM.js index 712ae00..ea6bcc0 100644 --- a/src/GCM.js +++ b/src/GCM.js @@ -1,9 +1,11 @@ "use strict"; -import Parse from 'parse/node'; +import Parse from 'parse'; +import log from 'npmlog'; import gcm from 'node-gcm'; import { randomString } from './PushAdapterUtils'; +const LOG_PREFIX = 'parse-server-push-adapter GCM'; const GCMTimeToLiveMax = 4 * 7 * 24 * 60 * 60; // GCM allows a max of 4 weeks const GCMRegistrationTokensMax = 1000; @@ -30,6 +32,7 @@ GCM.prototype.send = function(data, devices) { // chunk if necessary let slices = sliceDevices(devices, GCMRegistrationTokensMax); if (slices.length > 1) { + log.verbose(LOG_PREFIX, `the number of devices exceeds ${GCMRegistrationTokensMax}`); // Make 1 send per slice let promises = slices.reduce((memo, slice) => { let promise = this.send(data, slice, timestamp); @@ -68,6 +71,8 @@ GCM.prototype.send = function(data, devices) { let promises = deviceTokens.map(() => new Parse.Promise()); let registrationTokens = deviceTokens; + let length = registrationTokens.length; + log.verbose(LOG_PREFIX, `sending to ${length} ${length > 1 ? 'devices' : 'device'}`); this.sender.send(message, { registrationTokens: registrationTokens }, 5, (error, response) => { // example response: /* @@ -80,6 +85,11 @@ GCM.prototype.send = function(data, devices) { {"error":"InvalidRegistration"}, {"error":"InvalidRegistration"}] } */ + if (error) { + log.error(LOG_PREFIX, `send errored: %s`, JSON.stringify(error, null, 4)); + } else { + log.verbose(LOG_PREFIX, `GCM Response: %s`, JSON.stringify(response, null, 4)); + } let { results, multicast_id } = response || {}; registrationTokens.forEach((token, index) => { let promise = promises[index]; diff --git a/src/ParsePushAdapter.js b/src/ParsePushAdapter.js index 6deba8e..3e6c67c 100644 --- a/src/ParsePushAdapter.js +++ b/src/ParsePushAdapter.js @@ -1,9 +1,12 @@ -const Parse = require('parse/node').Parse; -const GCM = require('./GCM'); -const APNS = require('./APNS'); - +'use strict'; +import Parse from 'parse'; +import log from 'npmlog'; +import APNS from './APNS'; +import GCM from './GCM'; import { classifyInstallations } from './PushAdapterUtils'; +const LOG_PREFIX = 'parse-server-push-adapter'; + export class ParsePushAdapter { supportsPushTracking = true; @@ -48,6 +51,7 @@ export class ParsePushAdapter { let sender = this.senderMap[pushType]; let devices = deviceMap[pushType]; if (!sender) { + log.verbose(LOG_PREFIX, `Can not find sender for push type ${pushType}, ${data}`) let results = devices.map((device) => { return Promise.resolve({ device, diff --git a/src/index.js b/src/index.js index aa7f7ad..3cfc83f 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,11 @@ // ParsePushAdapter is the default implementation of // PushAdapter, it uses GCM for android push and APNS // for ios push. +import log from 'npmlog'; + +if (process.env.VERBOSE || process.env.VERBOSE_PARSE_SERVER_PUSH_ADAPTER) { + log.level = 'verbose'; +} import ParsePushAdapter from './ParsePushAdapter'; import GCM from './GCM';