我正在使用phonegap-push-plugin 2.0.0和FCM。发送通知时,Android应用程序崩溃。以下是相关代码。
config.xml文件是
<platform name="android">
<resource-file src="google-services.json" target="/google-services.json" />
</platform>
<plugin name="cordova-plugin-device" spec="2.0.2" source="npm" />
<plugin name="phonegap-plugin-push" spec="2.0.0" source="npm">
<variable name="SENDER_ID" value="1:XXXXXXXX:android:YYYYYYY" />
</plugin>
在Javascript代码中:
function onDeviceReady()
{
registerForFCMPushNotifications();
//... more code
}
function registerForFCMPushNotifications()
{
var notificationObj = PushNotification.init({
android: {
senderID: "XXXXXXXXX",
badge: "true",
sound: "true",
vibrate: "true",
topics: ["all"],
forceShow: "false"
},
ios: {
},
windows: {}
});
notificationObj.on('registration', function(data) {alert('Id='+data.registrationId);});
notificationObj.on('notification', function(data) {alert("Got notification");});
notificationObj.on('error', function(e) {alert( e.message );});
}
发件人ID已从FCM控制台本身获取。
观察到的行为:
如果在我的应用程序运行时发送了通知,那么它会发出一条消息,不幸的是该应用程序已停止。
如果在应用未运行时发送通知,则手机上不会显示任何通知。
请指教。
最佳答案
从资源文件/google-services.json中删除/
,使其成为
<resource-file src="google-services.json" target="google-services.json" />
确保google-services.json在根目录中
在config.xml中
<plugin name="phonegap-plugin-push" spec="^2.0.0">
<variable name="SENDER_ID" value="XXXXXXXXXX" />
</plugin>
在package.json中还包含SENDER_ID(如果用于其他构建)
"phonegap-plugin-push": {
"SENDER_ID": "XXXXXXXXXX"
}