问题描述
FCM推送通知如何工作?
看看下面的图片,我在互联网上解释了自己,这是不言自明的。首先,您需要在Google服务(FCM)中注册您的客户端android手机/应用,您将在其中获得唯一的ID,作为回报,您将发送自己的主服务器进行保存。因此,每次您要向安装了应用程序的android设备推送内容时,您的网络界面都将需要它已经拥有的每部手机的电话/应用程序唯一ID,以便您的服务器将通知推送到Google服务(FCM)并使用目标电话/应用程序的唯一ID,那么Google(无论如何)都会将通知发送到该电话,即使该电话未在前端运行您的应用程序,他们也会收到通知。
我的问题/问题?
我尝试使用具有以下文件/文件夹级别的Adobe PhoneGap Desktop创建一个简单的应用程序。
> My Project/
> platforms/
> browser/
> platforms.json
> plugins/
> cordova-plugin-fcm/
> cordova-plugin-whitelist/
> browser.json
> fetch.json
> www/
> res/
> icon/
> screen/
> index.html
> config.xml
> google-services.json
尝试过的其他插件:
config.xml
<?xml version='1.0' encoding='utf-8'?>
<widget id="net.exeideas.app" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
<name>EXEIdeas Push Notification</name>
<description>
A Basic Adobe PhoneGap For Push Notification Via PHP And Firebase Cloud Messaging.
</description>
<author email="[email protected]" href="httpd://www.exeideas.net">
PhoneGap Team
</author>
<content src="index.html" />
<preference name="android-minSdkVersion" value="14" />
<plugin name="cordova-plugin-whitelist" source="npm" spec="~1.2.1" />
<plugin name="cordova-plugin-fcm" source="npm" spec="~2.1.2" />
<platform name="android">
<icon src="www/res/icon/android/fcm_push_icon.png" />
<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" />
</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>
</widget>
index.html
<!DOCTYPE html>
<html>
<head>
<Meta charset="utf-8">
<Meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no,width=device-width">
<title>EXEIdeas Push Notofocation</title>
<script type="text/javascript">
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady() {
var push = PushNotification.init({ "android": {"senderID": "my sender id"}});
push.on('registration',function(data) {
console.log(data.registrationId);
});
push.on('notification',function(data) {
console.log(data.title + " Message: " + data.message);
});
push.on('error',function(e) {
console.log(e);
});
}
</script>
</head>
<body>
<h1>EXEIdeas Push Notification Example</h1>
<script type="text/javascript">
FCMPlugin.getToken(function(token) {
//this is the FCM token which can be used
//to send notification to specific device
alert(token);
FCMPlugin.onNotification( onNotificationCallback(data),successCallback(msg),errorCallback(err) )
//Here you define your application behavIoUr based on the notification data.
FCMPlugin.onNotification(function(data) {
alert(data);
//data.wasTapped == true means in Background : Notification was received on device tray and tapped by the user.
//data.wasTapped == false means in foreground : Notification was received in foreground. Maybe the user needs to be notified.
if (data.wasTapped) {
//Notification was received on device tray and tapped by the user.
alert(JSON.stringify(data));
} else {
//Notification was received in foreground. Maybe the user needs to be notified.
alert(JSON.stringify(data));
}
});
});
//FCMPlugin.onTokenRefresh( onTokenRefreshCallback(token) );
//Note that this callback will be fired every time a new token is generated,including the first time.
FCMPlugin.onTokenRefresh(function(token){
alert( token );
});
</script>
</body>
</html>
然后通过Adobe PhoneGap Desktop并直接在我的Android中运行此代码,如下面的屏幕快照所示。另外,我使用的<preference name="android-minSdkVersion" value="14" />
安卓SDK版本可以根据需要进行更改。
做完所有重要的事情之后,我正在使用cordova-plugin-fcm tester发送测试通知,但没有收到,并且在任何地方都看不到任何错误。提到的链接警报Push notification sent successfully!
。
那么有人能找出我在哪里做错了吗,因为我没有像在index.html
页中那样在Apps Desktop POPUP中获得设备ID?在某些博客上,我读到“推送通知”无法在Adobe PhoneGap Desktop Localhost上运行,因此我必须构建最终的APK进行测试,但我尚未这样做。另外,如果您认为我在FCM设置中犯了任何错误,那么您也可以提及这些步骤,我会再做一次。
注意:此网站上有很多问题看起来像这样,但是由于Cordova,Android,FCM等的更新,它们还是很老的,所以请不要在这里提及任何旧的答案。
解决方法
我有同样的问题,firebase 通知推送不再适用于使用 JavaScript(ionic、cordova、phonegap 等)的混合移动应用程序
我建议您使用 Android Native 应用程序(Java 或 Kotlin)