我使用 DevExtreme 开发了我的应用程序,这是一个基于 PhoneGap 的多平台工具。
现在,我尝试使用phonegap-plugin-push 管理推送通知。
我的第一个简单目标是发送接收来自 FCM(Firebase 云消息传递)的一些通知。
我更喜欢从 Android 开始,因此,我在 FCM 上设置了我的 Android 应用程序。在这里,我获取了发件人 ID。
根据文档,我修改了 config.xml,如下所示:
<widget id="com.devexpress.apptemplate" version="1.0" versionCode="1">
<name>ApplicationTemplate</name>
<description>Template</description>
<preference name="phonegap-version" value="cli-6.4.0" />
<preference name="permissions" value="none" />
<preference name="prerendered-icon" value="true" />
<preference name="android-windowSoftInputMode" value="adjustPan" />
<preference name="SplashScreen" value="splash" />
<preference name="SplashScreenDelay" value="60000" />
<preference name="AutoHideSplashScreen" value="false" />
<preference name="SplashShowOnlyFirstTime" value="false" />
<preference name="FadeSplashScreen" value="false" />
<preference name="ShowSplashScreenSpinner" value="false" />
<preference name="DisallowOverscroll" value="true" />
<preference name="StatusBarOverlaysWebView" value="false" />
<preference name="StatusBarBackgroundColor" value="#000000" />
<preference name="android-minSdkVersion" value="15" />
<preference name="android-targetSdkVersion" value="22" />
<!--<plugin name="cordova-plugin-file" />-->
<plugin name="cordova-plugin-geolocation" />
<plugin name="cordova-plugin-splashscreen" onload="true" />
<plugin name="cordova-plugin-whitelist" />
<plugin name="cordova-plugin-ios-longpress-fix" />
<plugin name="cordova-plugin-statusbar" onload="true" />
<plugin spec="https://github.com/phonegap/phonegap-plugin-push.git" source="git" >
<param name="SENDER_ID" value="123456" />
</plugin>
<access origin="*" />
</widget>
然后,在 index.js 文件的 deviceReady 事件中:
var push = PushNotification.init({
android: {
senderID: "123456"
},
browser: {
pushServiceURL: 'https://fcm.googleapis.com/fcm/send'
},
ios: {
alert: "true",
badge: "true",
sound: "true"
},
windows: {}
});
push.on('registration', function (data) {
// data.registrationId
DevExpress.ui.notify("Device registered", "success", 3000);
});
push.on('notification', function (data) {
// data.message,
// data.title,
// data.count,
// data.sound,
// data.image,
// data.additionalData
DevExpress.ui.notify(data.message, "info", 10000);
});
push.on('error', function (e) {
// e.message
DevExpress.ui.notify(e.message, "error", 10000);
});
痛苦就从这里开始。
首先我不知道pushServiceURL是否正确。如果我想从 FCM 发送一些通知,这是要使用的 URL 吗?
然后,我正确创建了应用程序模板并构建了 apk。但是,当然,当我将其安装在 Android 设备上并尝试从 FCM 发送通知时,我在应用程序上没有看到任何内容。
此外,我试图在应用程序启动后使用消息来管理注册事件,但是我也没有看到该消息.
所以,这里没有任何作用!恕我直言,由于缺乏文档,你能帮助我吗?
UPDATE:根据phonegap插件推送的文档,我注意到我必须包含google-service.json。所以,我在 config.xml 中写道:
<platform name="android">
<resource-file src="google-services.json" target="google-services.json" />
</platform>
我更改了index.js中的代码:
var push = PushNotification.init({
android: {},
ios: {
alert: "true",
badge: "true",
sound: "true"
},
windows: {}
});
由于 senderID 现在位于 google-services.json 中。此外,我还在 config.xml 中删除了 senderID:
<plugin spec="https://github.com/phonegap/phonegap-plugin-push.git" source="git" />
我还在我的项目中的index.html 中包含了这个文件(https://github.com/phonegap/phonegap-plugin-push/blob/master/src/js/push.js)但不知道是否正确。