未调用FCM android onmessagereceived
我已经在应用程序中实现了FCM。其他firebase功能工作正常,即上传到firebase存储和firebase实时消息。但是,当我第一次向设备发送推送通知时,它显示已成功发送通知,但未调用messagereceived。然后,当我发送另一个推送通知时,它立即显示未注册。而且之后总是没有注册未调用FCM android onmessagereceived,android,firebase,push-notification,firebase-cloud-messaging,Android,Firebase,Push Notification,Firebase Cloud Messaging,我已经在应用程序中实现了FCM。其他firebase功能工作正常,即上传到firebase存储和firebase实时消息。但是,当我第一次向设备发送推送通知时,它显示已成功发送通知,但未调用messagereceived。然后,当我发送另一个推送通知时,它立即显示未注册。而且之后总是没有注册 public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMes
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("myLog", "From: " + remoteMessage.getFrom());
Log.d("myLog", "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
}
app.gradle:
apply plugin: 'com.google.gms.google-services'
project.gralde:
classpath 'com.google.gms:google-services:3.1.0'
舱单:
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
设备令牌被正确刷新,当我清除数据并重新打开应用程序时,它会立即打印新的设备令牌 您使用什么设备来测试此功能?众所周知,小米和乐视等一些制造商阻止应用程序接收FCM通知 对于三星、摩托罗拉等设备:确保您处理的信息类型正确。您可以发送两种类型的FCM消息
这取决于来自控制台的消息类型。 如果只有通知消息,则如果您的应用程序已关闭,则不会触发接收器。 如果它包含通知消息和数据消息,那么当应用程序位于前台时,您的应用程序仍然可以处理这些消息 确保您正在使用此依赖项
编译'com.google.firebase:firebase messaging:10.2.1'
onMessageReceived(RemoteMessage RemoteMessage)方法,该方法基于以下情况调用
- FCM响应带有通知和数据块:
- FCM响应仅包含数据块:
private void sendNotification(Bitmap bitmap, String title, String
message, PendingIntent resultPendingIntent) {
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
style.bigPicture(bitmap);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = mContext.getString(R.string.default_notification_channel_id);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "channel_name", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
}
Bitmap iconLarge = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.mdmlogo);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.mdmlogo)
.setContentTitle(title)
.setAutoCancel(true)
.setSound(defaultSound)
.setContentText(message)
.setContentIntent(resultPendingIntent)
.setStyle(style)
.setLargeIcon(iconLarge)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_MAX)
.setChannelId(NOTIFICATION_CHANNEL_ID);
notificationManager.notify(1, notificationBuilder.build());
}
参考链接:
我已经检查了大多数设备,如moto X play、三星s6等。更新了我的答案。显示您的设备注册码