2017-07-01 61 views
0

我跟着tutorial on the PhoneGap documentation,他們的示例應用程序可以正常工作,但是當製作我自己的應用程序時,我無法弄清楚如何將PushNotification對象放入我的單頁應用程序中。如何在沒有推送服務的情況下觸發phonegap應用程序的通知?

我猜測的PhoneGap是基於config.xml中建設一個神祕cordova.js文件,並且該PushNotification加入到這個神祕文件中的根window範圍。

到目前爲止,我已經試過......

  • 包括我WWW/index.html的
  • 確信<plugin name="phonegap-plugin-push" ...是我的config.xml文件<script src="cordova.js"></script>
  • 在我的應用程序的JavaScript中添加了if (typeof PushNotification === 'undefined') { alert("No PushNotification"); }。無論是在瀏覽器本地測試(cordova文件顯然未找到),還是通過Android設備上的PhoneGap應用進行測試,這似乎都會觸發。

回答

0
push.on('notification', function(data) { 
    console.log('notification event'); 
    navigator.notification.alert(
    data.message,   // message 
    null,     // callback 
    data.title,   // title 
    'Ok'     // buttonName 
); 
}); 

上述事件處理程序將被觸發稱爲當應用程序接收到一個推送通知(該通知事件)。

當您使用phonegap-plugin-push時,它是如何開發的。 你不能否則觸發它。只有在收到推送通知時纔會發生。

您可以添加插件並在您的應用程序中添加這些行。這應該正確接收通知。 使用兩種或科爾多瓦PhoneGap的根據你的應用程序中加入插件,

cordova plugin add phonegap-plugin-push 
phonegap plugin add phonegap-plugin-push 

然後直接發起PushNotificationjs文件OnDeviceReady後,

var push = PushNotification.init({ 
    android: { 
     "senderID": "XXXXXXXX" //Your sendeer ID from Firebase Cloud Messenger (FCM) 
    }, 
    browser: { 
     pushServiceURL: 'http://push.api.phonegap.com/v1/push' 
    }, 
    ios: { 
     alert: "true", 
     badge: "true", 
     sound: "true" 
    }, 
    windows: {} 
}); 

push.on('registration', function(data) { 
    // data.registrationId 
    //store this in your server for future use 
}); 

push.on('notification', function(data) { 
    // data.message, 
    // data.title, 
    // data.count, 
    // data.sound, 
    // data.image, 
    // data.additionalData 
}); 

push.on('error', function(e) { 
    // e.message 
}); 
+0

謝謝。我遇到的問題是'PushNotification'是未定義的,即使''phonegap-plugin-push「'在'config.xml'中,我試圖在'deviceready'之後設置通知...仍然沒有'PushNotification'。這可能是因爲我正在測試應用程序的本地Web版本。 – Luke

+0

ohh好吧,以防萬一: - 在運行應用程序之前,檢查設備上的網絡連接。 'init()'函數需要Internet才能在FCM上註冊設備。我忘了這麼多次,並得到'PushNotification不可用'錯誤.. –

相關問題