Phonegap-plugin-push's on.('notification') doesn't fire
我正在尝试使用phonegap-plugin-push在我的混合应用程序中实现推送通知,该应用程序是由reactjs和cordova构建的。
我已经正确设置了所有内容(已添加插件,已在firebase中注册,google-services.js文件在正确的位置)。
我将其放在我的index.js文件中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import * as serviceWorker from './serviceWorker'; // ReactDOM.render(<App />, document.getElementById('root')); // If you want your app to work offline and load faster, you can change // unregister() to register() below. Note this comes with some pitfalls. // Learn more about service workers: const startApp = () => { ReactDOM.render( <App />, document.getElementById('root') ); serviceWorker.unregister(); var push = window.PushNotification.init({ android:{} }); console.log(push); push.on('registration', function (data) { console.log("on registration"); console.log(data); }); push.on('notification', function (data) { // console.log("notification received"); alert("Title:"+data.title+" Message:"+ data.message); }); push.on('error', function (e) { console.log(e.message) }); } if(!window.cordova) { console.log("cordova is null"); startApp(); } else { document.addEventListener('deviceready', startApp, false); } |
当我在android模拟器上运行该应用程序并使用chrome inspect设备对其进行调试时,我可以看到on('registration')被触发并正常工作。但是,当我尝试将通知从Firebase发送到设备时,什么也没发生。
这是我撰写通知的方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | *Notification title(optional): title Notification text: test noti Notification label: test noti *Target App com.allen.thomas.netdi //the package name that i registered *Scheduling i chose"send now" *Conversion events(optional) didn't do anything with this *Additional options(optional) //left the Android"Notification Channel" field blank //For custom data I filled in the following keys-values title Test Notification body Please work! badge 1 content-available 1 priority: high sound: enabled expires: 4 weeks |
然后我点击发布。但是什么也没发生。我不明白这里出了什么问题?
看来我想出了解决办法。我要做的就是在on('notification)的回调函数中添加以下代码行:
1 2 3 | push.finish(function(){ console.log("notification received successfully"); }) |
它解决了我的问题。
您的软件包名称是否与Firebase中的项目匹配?
您是否已在Android Studio中启用Android支持存储库版本47?
参见:https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/INSTALLATION.md#android-details