2014-01-15 2 views
6

Phonegap Android 앱에서 푸시 알림을 구현 중입니다. 튜토리얼 here을 따르고 있습니다. 튜토리얼에서 onDeviceready 기능은 다음과 같습니다 :Android Phonegap 앱에서 푸시 알림 메시지 처리

onDeviceReady: function() { 
    app.receivedEvent('deviceready'); 
    var pushNotification = window.plugins.pushNotification; 
    pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"836557454855","ecb":"app.onNotificationGCM"}); 
}, 

이가 푸시 알림을 위해 Google에 등록되어 때마다 앱이 시작을 의미합니다. 나는 이것이 단지 한 번만 할 필요가 있다고 추정한다. 그래서 내에서, 내가 가진 :

onNotificationGCM: function(e) { 

    switch(e.event) 
    { 
     case 'registered': 
      if (e.regid.length > 0) 
      { 
        /* 
        save reg id to server and store response in local storage 
        ... 
        ... 
        ... 
        */ 

        window.localStorage.setItem('mountmercy_device_id', data.id); 
        window.localStorage.setItem('mountmercy_api_key', data.get('api_key')); 

      } 
      break; 

     case 'message': 
      // this is the actual push notification. its format depends on the data model from the push server 
      alert('message = '+e.message+' msgcnt = '+e.msgcnt); 

      break; 

     case 'error': 
      //alert('GCM error = '+e.msg); 
      break; 

     default: 
      // alert('An unknown GCM event has occurred'); 
      break; 
    } 
}, 

전화가 새로운 푸시 알림을 수신 할 때 문제가 발생 장치가 다시 등록되지 않도록

onDeviceReady: function() { 

     var device_id = window.localStorage.getItem('mountmercy_device_id'); 

     //if device_id is in local storage, then it means registration 
     // with google has already taken place. If not, then register 
     if(typeof(device_id)==='undefined' || device_id===null){ 

      var pushNotification = window.plugins.pushNotification; 
      if (window.device.platform == 'android' || window.device.platform == 'Android') { 
       pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"475226855592","ecb":"app.onNotificationGCM"});       
      } 
      else{ 
       //so its apple 
       pushNotification.register(app.tokenHandler,app.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"}); 
      } 

     } 

} 

그런 다음 onNotificationGCM에, 나는 로컬 스토리지를 설정합니다. 튜토리얼의 원래 프로젝트에서 사용자가 알림 메시지를 클릭하면 앱이 열리고 사용자에게 경고 메시지가 표시됩니다. "message = blachblah msgcnt = blahblah". 이것은 onNotificationGCM()의 "message"케이스에있는 코드가 실행되기 때문입니다.

내 앱에서는 앱이 열리지 만 '메시지'의 코드는 실행되지 않습니다. 이것은 onDeviceReady()에서 Google에 한 번만 장치를 등록하기 때문입니다. 내가 조건을 제거하는 경우 :

if(typeof(device_id)==='undefined' || device_id===null){ 

을하고 디바이스를 구비마다 등록, "메시지"경우 실행됩니다. 하지만이 때마다 장치를 onDeviceReady()에 등록해야하는 것은 잘못되었습니다. 다른 해결책이 있습니까?

답변

3

내가 그것을

구글이 주기적으로이 경우 등록 ID

를 새로 고칠 수 있습니다 말한다 Google 클라우드 메시징의 문서를 참조하십시오 나는 구글 앱을 열 때마다 등록하고 변경된 경우 반환 된 등록 ID를 업데이트하십시오. 또한 푸시 메시지가 알림 표시 줄에서 클릭 될 때 처리하는 문제를 해결합니다.