2014-06-05 4 views
1

그래서, 폰갭에있어, 내가 사용Phonegap PushNotification, 내 노드 push-server를 phonegap과 함께 사용하는 방법?

그래서, 내가 첫 번째 플러그인을 설치하고 난 내 전화 토큰을 얻을 수 있습니다. 그 후, 나는 내 루트 디렉토리에 노드 server.js 파일을 만든 : 내가 명령 줄에서 node server.js 내 서버를 시작하면

var apn = require('apn'); 

var token = "MY TOKEN"; 
var device = new apn.Device(token); 

var notification = new apn.Notification(); 
notification.expiry = Math.floor(Date.now()/1000) + 3600; 
notification.badge = 1; 
notification.alert = "This is a Push Notification=)"; 
notification.payload = {'prop': 'special value'}; 
notification.device = device; 

var options = { 
gateway: 'gateway.sandbox.push.apple.com', 
cert: 'CER.pem', 
key: 'KEY.pem', 
passphrase: 'password' 
} 
var apnsConnection = new apn.Connection(options); 
apnsConnection.pushNotification(notification, device); 

, 그래서 모든 것이 괜찮아, 내 휴대 전화에서 내 푸시 알림을 볼 수 있습니다.

내 질문에, 내 코드 (phonegap) 다른 위치에 밀어 넣기 알림을 보내야합니다. 내가 어떻게 할 수 있니?

server.js이 실행 중일 때 어떻게 내 전화 갭 애플리케이션에서 다른 푸시 알림을 보낼 수 있습니까?

답변

0

위에서 수집 한 코드는 여러 번 호출 할 수있는 함수를 제공합니다. 예를 들어, 매우 간단한 구현은 다음과 같습니다

var apn = require('apn'); 
var options = { 
    gateway: 'gateway.sandbox.push.apple.com', 
    cert: 'CER.pem', 
    key: 'KEY.pem', 
    passphrase: 'password' 
}; 
var apnsConnection = new apn.Connection(options); 

module.exports.pushNotification = function(token, alert) { 
    var device = new apn.Device(token); 

    var notification = new apn.Notification(); 
    notification.alert = alert; 
    notification.device = device; 

    apnsConnection.pushNotification(notification, device); 
}; 

는 "푸시 알림 서비스"에 대한이 파일 pns.js 이름을 상상해보십시오. 당신이 server.js을 실행할 때
var pns = require("./pns.js"); 

pns.pushNotification("MY TOKEN", "This is a Push Notification"); 

이제 당신은 동일한 기능을 가지고 : 이제 server.js에 대신 require pushNotification 기능을 방금 만든 모듈을 호출 할 수 있었다. 여기서는 Node.js 측면에서이 함수를 호출해야하는 다른 모듈에이 함수를 가져올 수 있습니다.

원격 프로세스에서 호출해야하는 경우 Express과 같은 웹 프레임 워크를 살펴보고 동일한 코드를 호출하는 API를 빌드 할 수 있습니다. 토큰 및 경고 메시지는이 함수 호출에 전달 될 수 있습니다. 이렇게하면 server.js이 요청을 수신하고 요청시 푸시 알림을 보내는 실행중인 웹 서버로 바뀔 수 있습니다.

0

약간 늦었지만 같은 질문을 가진 사람이,이 도구보고를 위해 :

https://www.npmjs.com/package/node-pushserver

를 ... 그것은 정확하게 당신이 원하는 않습니다. iOS와 Android를 모두 지원합니다.

서버에서 실행하면 앱에서 다음을 수행 할 수 있습니다. http://yourserver:8000:/subscribe에 게시하여 기기를 등록 할 수 있습니다. 장치는 mongodb 데이터베이스에 저장됩니다. HTTP 요청을 http://yourserver:8000/send에 게시하면 등록 된 단일 장치, 하위 집합 또는 모두에 푸시 알림을 보낼 수 있습니다.

재미있게 보내세요!

관련 문제