无法集成 phonegap-plugin-push

Posted

技术标签:

【中文标题】无法集成 phonegap-plugin-push【英文标题】:unable to integrate phonegap-plugin-push 【发布时间】:2017-08-26 06:00:38 【问题描述】:

下面是我在 www 文件夹中 index.html 的代码,发件人 ID 已根据需要从 console.firebase.google.com 更改。但是当我尝试通过 phonegap 桌面应用程序连接和运行应用程序时仍然没有收到任何通知。 抱歉,这是我第一次在应用程序中集成推送通知。 所以我需要你的帮助。 在此先感谢

<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" />
<!-- This is a wide open CSP declaration. To lock this down for production, see below. -->
<meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
</head>

<body>
<div class="app">
<h1>PhoneGap</h1>
<div id="registration" class="blink">
<p class="event waiting">Requesting Registration ID</p>
<p class="event received">Registered</p>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>

</html>

下面是我的 javascript (index.js) 代码

var app = 
// Application Constructor
initialize: function() 
this.bindEvents();
,
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() 
document.addEventListener('deviceready', this.onDeviceReady, false);
,
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() 
console.log('Received Device Ready Event');
console.log('calling setup push');
app.setupPush();
,
setupPush: function() 
console.log('calling push init');
var push = PushNotification.init(
"android": 
"senderID": "XXXXXXX"
,
"browser": ,
"ios": 
"sound": true,
"vibration": true,
"badge": true
,
"windows": 
);
console.log('after init');

push.on('registration', function(data) 
console.log('registration event: ' + data.registrationId);
var oldRegId = localStorage.getItem('registrationId');
if (oldRegId !== data.registrationId) 
// Save new registration ID
localStorage.setItem('registrationId', data.registrationId);
// Post registrationId to your app server as the value has changed


var parentElement = document.getElementById('registration');
var listeningElement = parentElement.querySelector('.waiting');
var receivedElement = parentElement.querySelector('.received');

listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
);

push.on('error', function(e) 
console.log("push error = " + e.message);
);

push.on('notification', function(data) 
console.log('notification event');
navigator.notification.alert(
data.message,         // message
null,                 // callback
data.title,           // title
'Ok'                  // buttonName
);
);

;

下面是我的 config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.phonegap.push" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
<name>phonegappush</name>
<description>
Push sample application that receives push notifications.
</description>
<author email="support@phonegap.com" href="http://phonegap.com">
PhoneGap Team
</author>
<content src="index.html" />
<preference name="DisallowOverscroll" value="true" />
<preference name="android-minSdkVersion" value="14" />
<preference name="StatusBarOverlaysWebView" value="false" />
<preference name="StatusBarBackgroundColor" value="#ee6e73" />
<preference name="StatusBarStyle" value="blacktranslucent" />
<plugin name="cordova-plugin-statusbar" source="npm" spec="~2.1.3" />
<plugin name="phonegap-plugin-push" source="npm" spec="~1.8.0">
<variable name="SENDER_ID" value="XXXXXXX" />
</plugin>
<plugin name="cordova-plugin-console" source="npm" spec="~1.0.3" />
<plugin name="cordova-plugin-dialogs" source="npm" spec="~1.2.1" />
<plugin name="cordova-plugin-whitelist" source="npm" spec="~1.2.1" />
<platform name="android">
<icon density="ldpi" src="www/res/icon/android/drawable-ldpi-icon.png" />
<icon density="mdpi" src="www/res/icon/android/drawable-mdpi-icon.png" />
<icon density="hdpi" src="www/res/icon/android/drawable-hdpi-icon.png" />
<icon density="xhdpi" src="www/res/icon/android/drawable-xhdpi-icon.png" />
<icon density="xxhdpi" src="www/res/icon/android/drawable-xxhdpi-icon.png" />
<icon density="xxxhdpi" src="www/res/icon/android/drawable-xxxhdpi-icon.png" />
<splash density="land-ldpi" src="www/res/screen/android/drawable-land-ldpi-screen.png" />
<splash density="land-mdpi" src="www/res/screen/android/drawable-land-mdpi-screen.png" />
<splash density="land-hdpi" src="www/res/screen/android/drawable-land-hdpi-screen.png" />
<splash density="land-xhdpi" src="www/res/screen/android/drawable-land-xhdpi-screen.png" />
<splash density="land-xxhdpi" src="www/res/screen/android/drawable-land-xxhdpi-screen.png" />
<splash density="land-xxxhdpi" src="www/res/screen/android/drawable-land-xxxhdpi-screen.png" />
<splash density="port-ldpi" src="www/res/screen/android/drawable-port-ldpi-screen.png" />
<splash density="port-mdpi" src="www/res/screen/android/drawable-port-mdpi-screen.png" />
<splash density="port-hdpi" src="www/res/screen/android/drawable-port-hdpi-screen.png" />
<splash density="port-xhdpi" src="www/res/screen/android/drawable-port-xhdpi-screen.png" />
<splash density="port-xxhdpi" src="www/res/screen/android/drawable-port-xxhdpi-screen.png" />
<splash density="port-xxxhdpi" src="www/res/screen/android/drawable-port-xxxhdpi-screen.png" />
<resource-file src="google-services.json" target="google-services.json" />
</platform>
<platform name="ios">
<icon  platform="ios" src="www/res/icon/ios/icon.png"  />
<icon  platform="ios" src="www/res/icon/ios/icon@2x.png"  />
<icon  platform="ios" src="www/res/icon/ios/icon-40.png"  />
<icon  platform="ios" src="www/res/icon/ios/icon-40@2x.png"  />
<icon  platform="ios" src="www/res/icon/ios/icon-50.png"  />
<icon  platform="ios" src="www/res/icon/ios/icon-50@2x.png"  />
<icon  platform="ios" src="www/res/icon/ios/icon-60.png"  />
<icon  platform="ios" src="www/res/icon/ios/icon-60@2x.png"  />
<icon  platform="ios" src="www/res/icon/ios/icon-60@3x.png"  />
<icon  platform="ios" src="www/res/icon/ios/icon-72.png"  />
<icon  platform="ios" src="www/res/icon/ios/icon-72@2x.png"  />
<icon  platform="ios" src="www/res/icon/ios/icon-76.png"  />
<icon  platform="ios" src="www/res/icon/ios/icon-76@2x.png"  />
<icon  platform="ios" src="www/res/icon/ios/icon-small.png"  />
<icon  platform="ios" src="www/res/icon/ios/icon-small@2x.png"  />
<icon  platform="ios" src="www/res/icon/ios/icon-small@3x.png"  />
<splash  platform="ios" src="www/res/screen/ios/Default-568h@2x~iphone.png"  />
<splash  platform="ios" src="www/res/screen/ios/Default-667h.png"  />
<splash  platform="ios" src="www/res/screen/ios/Default-736h.png"  />
<splash  platform="ios" src="www/res/screen/ios/Default-Landscape-736h.png"  />
<splash  platform="ios" src="www/res/screen/ios/Default-Landscape@2x~ipad.png"  />
<splash  platform="ios" src="www/res/screen/ios/Default-Landscape~ipad.png"  />
<splash  platform="ios" src="www/res/screen/ios/Default-Portrait@2x~ipad.png"  />
<splash  platform="ios" src="www/res/screen/ios/Default-Portrait~ipad.png"  />
<splash  platform="ios" src="www/res/screen/ios/Default@2x~iphone.png"  />
<splash  platform="ios" src="www/res/screen/ios/Default~iphone.png"  />
</platform>
<platform name="wp8">
<icon  platform="wp8" src="www/res/icon/wp8/ApplicationIcon.png"  />
<icon  platform="wp8" src="www/res/icon/wp8/Background.png"  />
<splash  platform="wp8" src="www/res/screen/wp8/screen-portrait.jpg"  />
</platform>
<platform name="windows">
<icon  platform="windows" src="www/res/icon/windows/Square150x150Logo.scale-100.png"  />
<icon  platform="windows" src="www/res/icon/windows/Square30x30Logo.scale-100.png"  />
<icon  platform="windows" src="www/res/icon/windows/StoreLogo.scale-100.png"  />
<splash  platform="windows" src="www/res/screen/windows/SplashScreen.scale-100.png"  />
<icon  platform="windows" src="www/res/icon/windows/StoreLogo.scale-240.png"  />
<icon  platform="windows" src="www/res/icon/windows/Square44x44Logo.scale-100.png"  />
<icon  platform="windows" src="www/res/icon/windows/Square44x44Logo.scale-240.png"  />
<icon  platform="windows" src="www/res/icon/windows/Square70x70Logo.scale-100.png"  />
<icon  platform="windows" src="www/res/icon/windows/Square71x71Logo.scale-100.png"  />
<icon  platform="windows" src="www/res/icon/windows/Square71x71Logo.scale-240.png"  />
<icon  platform="windows" src="www/res/icon/windows/Square150x150Logo.scale-240.png"  />
<icon  platform="windows" src="www/res/icon/windows/Square310x310Logo.scale-100.png"  />
<icon  platform="windows" src="www/res/icon/windows/Wide310x150Logo.scale-100.png"  />
<icon  platform="windows" src="www/res/icon/windows/Wide310x150Logo.scale-240.png"  />
<splash  platform="windows" src="www/res/screen/windows/SplashScreenPhone.scale-240.png"  />
</platform>
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<allow-intent href="market:*" />
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
</platform>
</widget>

【问题讨论】:

依次排查问题,你在console中的每个阶段发现了什么? 我检查警报,到处都是警报。除了 push.on('notification', function(data) @Jobayer 虽然我不是使用控制台检查的专家,但我可以检查的内容是“未捕获的 ReferenceError:PushNotification 未在 Object.setupPush (index.js:24) 处定义Channel.onDeviceReady (index.js:20) at Channel.fire (cordova.js:777) at cordova.js:231" 没有正确找到 PushNotification 插件,尝试使用他们的官方文档解决这个问题。 @Jobayer 从过去三天开始一直在努力使其工作,并尝试一切都已阅读在 *** 中检查的可用文档,但无法使其工作。 【参考方案1】:

我已经为 phoneGap 应用程序完成了它,在配置文件中使用它来推送通知插件 &lt;plugin name="phonegap-plugin-push" spec="~1.10.5" source="npm" /&gt; 这是我的初始化代码

var push = PushNotification.init(
                "android": "senderID": "your sender id", "sound": true, 
"vibrate": true,
                "ios": "alert": true, "badge": true, "sound": true, 
"clearBadge": true
            );

这是为了通知

push.on('notification', function(data) 
                alert(JSON.stringify(data));
                setTimeout(function()
                    $("#notification_button").trigger("click");
                , 1000);
            );

【讨论】:

仍然没有成功:( . 永远不会进入 push.on('notification', function(data) . push.on('registration', function(data) 正在工作,我可以在这个函数中提醒到最后一个。 您是否将设备数据保存到数据库以发送通知? 我现在正在使用 cordova-plugin-fcm 并且现在能够接收通知,但是 FCMPlugin.getToken( 从来没有被调用,因为我想要设备的令牌,我尝试准备就绪,毕竟脚本结束但没有成功。【参考方案2】:

除非我遗漏了什么,否则在您的代码中,我看不到您存储用户设备 ID 的任何地方!

如果您不保存他们的设备 ID,您希望如何向他们发送 PN?!

我个人使用 php 来发送 PN。

在您的 config.xml 文件中使用它:

<plugin name="phonegap-plugin-push" source="npm" spec="~1.4.2">
    <param name="SENDER_ID" value="xxxxxxx" />
</plugin>

您需要在您的 google firebase 控制台中生成发件人 ID。

把它放在你的 html 文件中:

var push = PushNotification.init(
    android: 
        senderID: "xxxxxxxxxxxx",
        sound: true,
        vibrate: true,
        alert: true
    ,
    browser: 
        pushServiceURL: 'http://push.api.phonegap.com/v1/push'
    ,
    ios: 
        alert: true,
        badge: true,
        sound: true
    ,
    windows: 
);

push.on('registration', function(data) 
console.log(data.registrationId);


var user = data.registrationId;


////Here you alert the users device ID... remove this alert in production//////

alert(user);

////Here we send the users ID to our server and will store in in our database so we can use the ID to send them the PN's/////////


var dataString="&user="+user+"&add=";

if($.trim(user).length>0)

$.ajax(
type: "POST",
url: 'https://yoursite.com/write.php',
data: dataString,
crossDomain: true,
cache: false,
beforeSend: function(),
success: function(data)

//alert(data);


);
 



);


////////Here we just show them the PN in alert(), use something more elegant to show them the PN.///////

push.on('notification', function(data) 

console.log(data.message);

data = data.message;

alert(data);



//alert(data.title+" Message: " +data.message);
// data.title,
// data.count,
// data.sound,
// data.image,
// data.additionalData
);

push.on('error', function(e) 
console.log(e.message);
);

完成。

编码愉快。

【讨论】:

感谢您的帮助,但我仍在努力更改发件人 ID,但这次没有警报 :( . 对不起,我真的不知道下一步该做什么,请你帮个忙。能否请您提供三个文件 index.html 、 index.js 和 config.xml 、write.php ,我可以将其放入并查看它是否正常工作,我将衷心感谢您,因为我已经为此苦苦挣扎了 10 多天,其余的一切都在应用程序中正常工作。请需要您的帮助。 从不进入,push.on('notification', function(data) ,用户注册ID也在localStorage.setItem('registrationId', data.registrationId); 已经如你所说它不在代码中。请指导。 首先调试您的代码并查看调试控制台。

以上是关于无法集成 phonegap-plugin-push的主要内容,如果未能解决你的问题,请参考以下文章

phonegap-plugin-push 消息未显示为 android

如何在IOS中安装phonegap-plugin-push

我在哪里放置 phonegap-plugin-push 2.0.0 的 SENDER_ID?

phonegap-plugin-push设置启动

phonegap-plugin-push 获取不是 FCM 令牌的 APN 令牌

在 Chrome 浏览器、iOS 和 Android 中使用 phonegap-plugin-push?