首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Phonegap-plugin-push开启。(‘通知’)不会触发

Phonegap-plugin-push开启。(‘通知’)不会触发
EN

Stack Overflow用户
提问于 2019-01-14 00:38:41
回答 2查看 808关注 0票数 0

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

我已经正确地设置了一切(添加了插件,注册到firebase,google-services.js文件在正确的位置)。

我在我的index.js文件中放了这个:

代码语言:javascript
复制
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向设备发送通知时,什么也没有发生。我的通知是这样写的:

代码语言:javascript
复制
*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

然后我放弃了发布。但什么都没发生。我不明白这里有什么问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-16 23:07:33

似乎我找到了解决这个问题的办法。我所需要做的就是在on(‘通知)的回调函数中添加以下代码行:

代码语言:javascript
复制
push.finish(function(){
  console.log("notification received successfully");
})

它解决了我的问题。

票数 0
EN

Stack Overflow用户

发布于 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54170941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档