2017-04-11 152 views
1

我正在使用科爾多瓦插件添加phonegap-plugin-push - 變量SENDER_ID = XXXXXXXX 這個插件。並將以下代碼添加到JavaScript文件中。在科爾多瓦的推送通知

function setupPush() { 
    var push = PushNotification.init({ 
     "android": { 
      "senderID": "XXXXXXXX" 
     }, 
     "ios": { 
     "sound": true, 
     "alert": true, 
     "badge": true 
     }, 
     "windows": {} 
    }); 

    push.on('registration', function(data) { 
     console.log("registration event: " + data.registrationId); 
     var oldRegId = localStorage.getItem('registrationId'); 
     if (oldRegId !== data.registrationId) { 
      // Save new registration ID 
      localStorage.setItem('registrationId', data.registrationId); 
      // Post registrationId to your app server as the value has changed 
     } 
    }); 

    push.on('error', function(e) { 
     console.log("push error = " + e.message); 
    }); 
} 

它給出的錯誤爲「TypeError:無法調用未定義的方法init」。

+0

我的回答對你的問題有幫助嗎? –

回答

0

如果您在onDeviceReady之後沒有完成此操作,通常會發生這種情況。在此之前,您無法訪問任何本機設備API(推送通知,相機等)。

function onDeviceReady() { 

    // Sets up your push notifications 
    setupPush(); 
} 

// Fires when device is ready. 
document.addEventListener("deviceready", onDeviceReady, false); 
相關問題