简体   繁体   English

Phonegap-plugin-push处于打开状态。(“ notification”)不会触发

[英]Phonegap-plugin-push's on.('notification') doesn't fire

I'm trying to implement push-notification in my hybrid app, which is built with reactjs and cordova, using phonegap-plugin-push. 我正在尝试使用phonegap-plugin-push在我的混合应用程序中实现推送通知,该应用程序是由reactjs和cordova构建的。

i have setup everything correctly (added the plugin, registered with firebase, the google-services.js file is in the right place). 我已经正确设置了一切(添加了插件,已在firebase中注册,google-services.js文件在正确的位置)。

i put this in my index.js file: 我把它放在我的index.js文件中:

 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); } 

when i run the app on android emulator, and debug it using the chrome inspect devices, i can see the on('registration') is fired and work properly. 当我在android模拟器上运行该应用程序,并使用chrome inspect设备对其进行调试时,我可以看到on('registration')被触发并正常工作。 But when i try to send a notification from firebase to the device, nothing happen. 但是,当我尝试将通知从Firebase发送到设备时,什么也没发生。 This is how i compose my notification: 这是我撰写通知的方式:

 *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 

then i hitted publish. 然后我点击发布。 But nothing happened. 但是什么也没发生。 I don't understand what is the problem here? 我不明白这是什么问题?

Does your packagename match with the project in firebase? 您的套件名称与Firebase中的项目相符吗?

Have you enabled Android Support Repository version 47+ in Android Studio? 您是否已在Android Studio中启用Android支持存储库47+版本? See: https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/INSTALLATION.md#android-details 参见: https : //github.com/phonegap/phonegap-plugin-push/blob/master/docs/INSTALLATION.md#android-details

It seems that i figure out the solution to this. 看来我想出了解决办法。 All i need to do was adding these lines of code inside the on('notification) 's callback function: 我需要做的就是在on('notification)的回调函数中添加以下代码行:

 push.finish(function(){ console.log("notification received successfully"); }) 

It solve my problem. 它解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM