public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
}
...
}
dependencies {
// Add the Firebase SDK for Google Analytics
classpath 'com.google.firebase:firebase-messaging:20.2.4'
}
// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'
另外,你需要在应用的Manifest文件中添加以下代码:
如果你已经完成了以上步骤,但仍无法接收推送通知,请检查你的设备是否已连接到网络,并且是否有任何防火墙或其他安全软件阻止了通知的传输。
上一篇:AVD无法访问互联网