首先,我读到一个与此问题类似的问题,但是它并没有解决我的问题。这是问题FCM question
我几乎实现了所有东西,但是我不知道是否遗漏了一些东西。我在API 27和28版本的设备上测试了通知,但没有成功。即使日志没有显示在onMessageReceived()
中,我也检查了google-service.json
文件,看起来还可以,所以想知道是什么问题吗?
下面是我的FirebaseInstance类实现
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("No messages why ?", "From: " + remoteMessage.getFrom());
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d("Horray", "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
showNotification(remoteMessage.getNotification().getTitle(),
remoteMessage.getNotification().getBody());
}
private void showNotification(String title, String body) {
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "huxy.fcm.com.newfcmapp";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
"Notification",NotificationManager.IMPORTANCE_DEFAULT );
notificationChannel.setDescription("EDMT Channel");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.setVibrationPattern(new long[]{0,1000,500,1000});
notificationChannel.enableLights(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setContentInfo("Info");
notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
}
可以从GitHub链接Firebase Messaging instance class
查看完整的类。然后我的Manifest
文件也可以从这里访问:
Manifest file
您还可以浏览我的整个项目,看看我是否做过任何错误,该项目只是测试Firebase云消息传递。没有其他功能。
下面是该项目的链接:
这是我用来发送消息的控制台:
这是我放置我的设备令牌的地方: