我正在开发一个使用 Firebase Cloud Messagging 的应用,但在远程服务器通过 FCM 发送的推送通知方面遇到了奇怪的问题。
我所说的奇怪行为是,当我在 USB 连接的手机 Oneplus2(Android 6.0.1,API 23)中运行我的应用程序时,一切都像魅力一样工作。但是在虚拟设备中运行(我试图创建其中的许多设备,具有不同的API版本和Android版本)似乎甚至没有执行FirebaseMessagingService的onMessageReceived功能。
我怀疑从 AVD Manager 创建的虚拟设备配置不佳,但根据 Firebase 文档我需要
- A device running Android 4.0 (Ice Cream Sandwich) or newer, and Google Play services 10.2.0 or higher
- The Google Play services SDK from the Google Repository, available in the Android SDK Manager
- The latest version of Android Studio, version 1.5 or higher
我对这三个必要条件很满意。
如有任何帮助,我们将不胜感激。
编辑
FirebaseMessagingService.java
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
showNotification(remoteMessage.getData().get("message"));
Log.i("PVL", "MESSAGE RECEIVED!!");
}
private void showNotification(String message) {
Intent i = new Intent(this, WalkersMain.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("Walkers")
.setContentText(message)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ctscts.game"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="24" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/GdxTheme"
>
<activity
android:name="com.ctscts.game.AndroidLauncher"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".FirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
注意:我尝试从 Firebase 控制台和远程服务器发送请求,行为相同。
我如何执行发送请求:
@RequestMapping(value = { "/send" }, method = RequestMethod.GET)
public ResponseEntity<Void> sentNotification(@RequestBody String body, UriComponentsBuilder ucBuilder) {
HttpHeaders headers = new HttpHeaders();
String serverKey = "AAAAfZXBxHk:APA91bFlZ6ty7DQbLZkF5LJJUQJbYlO6JuJ8JmcQetG_cyAYD69QvPT6KK3KWkfTWHEWfgrwg7C7GSS1rWlNcumNNmkWfVae5p89";
List<User> lista = userService.findAllUsers();
List<String> listaToken = new ArrayList<String>();
for (User user : lista) {
listaToken.add(user.getToken());
}
Sender sender = new FCMSender(serverKey);
Message message = new Message.Builder()
.collapseKey("message")
.timeToLive(3)
.delayWhileIdle(true)
.addData("message", "Notification from Java application")
.build();
MulticastResult result;
try {
result = sender.send(message, listaToken, 1);
System.out.println("Result: " + result.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ResponseEntity<Void>(headers, HttpStatus.NO_CONTENT);
}
在 Debug模式下,当我执行该行 result = sender.send(message, listaToken, 1); message 变量的值为
Message(collapseKey=message,
timeToLive=3,
delayWhileIdle=true,
data: {
message=Notification from Java application
}
)
listaToken 包含要到达的 token 列表。 结果如下:
结果:
MulticastResult(multicast_id=9084949851503613851,total=1,success=1,failure=0,canonical_ids=0,results: [[ messageId=0:1489049633129649%6a8d477338eb0007 ]]
最佳答案
终于找到解决办法了。 根据Firebase Documentation :
If your organization has a firewall that restricts the traffic to or from the Internet, you need to configure it to allow connectivity with FCM in order for your Firebase Cloud Messaging client apps to receive messages. The ports to open are: 5228, 5229, and 5230. FCM typically only uses 5228, but it sometimes uses 5229 and 5230. FCM doesn't provide specific IPs, so you should allow your firewall to accept outgoing connections to all IP addresses contained in the IP blocks listed in Google's ASN of 15169.
我公司的防火墙阻止了这些端口上的网络流量,因此我尝试使用没有网络限制的 VPN 服务,一切正常。
关于android - Firebase 通知在 Android 虚拟设备上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42669533/