我正在尝试在我的混合应用中实现推送通知,该应用是用reactjs和cordova构建的,使用phonegap-plugin-push。
我已经正确地设置了一切(添加了插件,注册到firebase,google-services.js文件在正确的位置)。
我在我的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);
}
当我在android模拟器上运行应用程序,并使用chrome inspect设备对其进行调试时,我可以看到on (‘注册’)被触发并正常工作。但是当我尝试从firebase向设备发送通知时,什么也没有发生。我的通知是这样写的:
*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
然后我放弃了发布。但什么都没发生。我不明白这里有什么问题?
发布于 2019-01-16 23:07:33
似乎我找到了解决这个问题的办法。我所需要做的就是在on(‘通知)的回调函数中添加以下代码行:
push.finish(function(){
console.log("notification received successfully");
})
它解决了我的问题。
发布于 2019-01-16 15:53:31
您的包名称是否与firebase中的项目匹配?
您是否在Android Studio中启用了Android Support Repository版本47+?请参阅:https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/INSTALLATION.md#android-details
https://stackoverflow.com/questions/54170941
复制相似问题