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

时间:2019-01-13 16:38:41

标签: javascript android cordova phonegap-plugins

我正在尝试使用phonegap-plugin-push在由reactjs和cordova构建的混合应用程序中实现推送通知。

我已经正确设置了所有内容(已添加插件,已在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('registration')被触发并正常工作。但是,当我尝试将通知从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

然后我点击发布。但是什么也没发生。我不明白这里出了什么问题?

2 个答案:

答案 0 :(得分:0)

您的软件包名称是否与Firebase中的项目匹配?

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

答案 1 :(得分:0)

看来我想出了解决办法。我要做的就是在on('notification)的回调函数中添加以下代码行:

public class PageResponseDto<T> extends ResponseDto<List<T>> {

    private int pageNum;

    private int pageSize;

    private int size;

    private long total;

    private int pages;

    public PageResponseDto() {
        super();
    }

    public PageResponseDto(PageInfo<T> p) {
        super(p.getList());
        this.pageNum = p.getPageNum();
        this.pageSize = p.getPageSize();
        this.size = p.getSize();
        this.total = p.getTotal();
        this.pages = p.getPages();
    }
}

它解决了我的问题。