我需要使用phonegap推送插件向我的应用发送通知,并从Firebase控制台发送通知。我还部署了谷歌加登录插件&使用Phonegap Build构建我的应用程序。当我的应用程序打开时,它首先检查谷歌登录,然后相应地进行。
无法获得注册ID 的的index.html
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="push.js"></script>
var push = PushNotification.init({
"android": {
"senderID": "xxxxxxxxxx"
},
});
push.on('registration', function(data) {
alert('registration event: ' + data.registrationId);
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) {
alert("push error = " + e.message);
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.myapp1" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
<name>MyApp</name>
<description>
My App
</description>
<author email="support@phonegap.com" href="http://phonegap.com">
sqlchild
</author>
<preference name="android-minSdkVersion" value="17" />
<preference name='phonegap-version' value='cli-8.0.0' />
<preference name='pgb-builder-version' value='2' />
<plugin name="cordova-plugin-splashscreen" source="npm" spec="5.0.2"/>
<hook src="scripts/cordova-google-services-version-gradle-fix.js" type="before_prepare" />
<platform name="android">
<!-- <framework src="com.google.android.gms:play-services-gcm:+" /> -->
<!-- <framework src="com.google.android.gms:play-services-gcm:11.8.0" /> -->
<framework src="com.google.android.gms:play-services-gcm:9.0.0" />
<!-- <framework src="com.android.support:support-v4:+" /> -->
<!-- <framework src="com.android.support:support-v4:11.8.0" /> -->
<framework src="com.android.support:support-v4:9.0.0" />
</platform>
<plugin name="phonegap-plugin-push" spec="2.1.3">
<param name="SENDER_ID" value="x:xxxxx:android:xxxxxx" />
</plugin>
<plugin name="cordova-plugin-googleplus" source="npm" spec="5.3.0"></plugin>
<platform name="android">
<resource-file src="app/google-services.json" target="app/google-services.json" />
</platform>
<content src="index.html" />
</widget>
答案 0 :(得分:0)
考虑到您已安装Push plugin。
您已在设备就绪事件后发出推送通知初始化调用。
<!DOCTYPE html>
<html>
<head>
<title>Device Ready Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" src="push.js"></script>
// Wait for device API libraries to load
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// device APIs are available
//
function onDeviceReady() {
// Now safe to use device APIs
var push = PushNotification.init({
"android": {
"senderID": "xxxxxxxxxx"
},
});
push.on('registration', function(data) {
alert('registration event: ' + data.registrationId);
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;');
});
}
</script>
</head>
<body onload="onLoad()">
</body>
</html>
在此之后,您将能够在控制台中看到reg id。
修改1 : - Push plugin installation guide