2017-01-18 65 views
0

我正試圖在我的應用中實現推送通知概念。 成功安裝插件後,push.on(registration)方法不調用如何在Cordova平臺中實現推送通知?

我的項目結構projectname/platforms/android/assets/www

www文件夾包含homepage.html

我都html,js,css文件

notification.js文件,我已經叫在中寫的代碼notification.js是:

document.addEventListener('deviceready', pushNotification, false); 
function PushNotification(){ 
var push = PushNotification.init({ "android": {"senderID": "GCMProjectId(123456789)"},"ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} }); 
     push.on('registration', function(data) { 
      alert("registration id is:"+data.registrationId); 
      var id = localStorage.getItem("userId"); 
      var notifyInput = { 
         "token":data.registrationId, 
         "type":"android", 
         "uid":id 
       } 
      }); 

    push.on('notification', function(data) { 
       alert(data.message); 
      }); 

    push.on('error', function(e) { 
      // e.message 
      alert("error function calling on push notifications"); 

      }); 

} 

這裏,push.on(registration) and push.on(notification)方法不打電話,請讓我們知道可能的方式來得到通知特定設備

回答

1

安裝成功推送通知插件後,我收到了通知,Android的

過程:

通過使用下面的鏈接我已經安裝上推通知插件


cordova plugin add https://github.com/phonegap/phonegap-plugin-push --variable SENDER_ID="xxxxxxxxxxxxxx"

安裝要求:

-Android版> 6.0.0
-iOS版本> 4.3.0更好

對於iOS版本,豆莢required.so我們需要安裝莢

sudo gem install cocoapods 

對於GCM註冊: https://developers.google.com/mobile/add

成功安裝完成後,將創建pod文件。一旦完成,打開project.xcworkspace文件。然後,iOS應用程序將正常運行

如果您在應用程序中調用的通知,然後寫addEventListener方法

document.addEventListener('deviceready', pushNotification, false); 

    function PushNotification(){ 
    var push = PushNotification.init({ "android": {"senderID": "xxxxxx(refers project number in GCM)"},"ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} }); 

      push.on('registration', function(data) { 
       alert("registration id is:"+data.registrationId); 
       // registration id need to pass your notification server 

       }); 

     push.on('notification', function(data) { 
        alert(data.message); 
        // you receive the notification 
       }); 

     push.on('error', function(e) { 
       // e.message 
       alert("error function calling on push notifications"); 

       }); 

    }