Java 未收到android fcm通知

Java 未收到android fcm通知,java,android,Java,Android,未收到android fcm通知,而应接收通知的设备在Logcat中显示此消息(从fcm标题接收:null,从fcm正文接收:null)。我已经检查了26个SDK版本都没有收到通知 ============================MyFirebaseMessagingService=============================== public class MyFirebaseMessagingService extends FirebaseMessagingService

未收到android fcm通知,而应接收通知的设备在Logcat中显示此消息(从fcm标题接收:null,从fcm正文接收:null)。我已经检查了26个SDK版本都没有收到通知

============================MyFirebaseMessagingService===============================

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String channel_id = "the_id";


@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    Log.e("NEW_TOKEN",s);
    updateTokenToFirebase(s);

}


private void updateTokenToFirebase(String token) {
    IDrinkShopAPI mService = Common.getAPI();

    mService.updateToken("SERVER_01",token,"0")
            .enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    Log.d("DEBUG_TOKEN",response.body());
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    Log.d("DEBUG_TOKEN",t.getMessage());
                }
            });


}



@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    if(remoteMessage.getData() != null){

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            sendNotification26(remoteMessage);
        else
            sendNotification(remoteMessage);

    }

}

private void sendNotification26(RemoteMessage remoteMessage) {
    Map<String,String> data = remoteMessage.getData();

    String title = data.get("title");
    String message = data.get("message");



    NotificationHelper helper ;
    Notification.Builder builder;
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    helper = new NotificationHelper(this);

    builder = helper.getDrinkShopNotification(title,message,defaultSoundUri);

    helper.getManager().notify(new Random().nextInt(),builder.build());


}


private void sendNotification(RemoteMessage remoteMessage) {

    Map<String,String> data = remoteMessage.getData();

   String title = data.get("title");
   String message = data.get("message");



    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri);

    NotificationManager mn =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    mn.notify(new Random().nextInt(),builder.build());

   }
}
//this class is used to implement notification for all android versions

public class NotificationHelper extends ContextWrapper {

private static final String CHANNEL_ID = "the_id";
private static final String CHANNEL_NAME = "Drink_Shop";

private NotificationManager notificationManager;

public NotificationHelper(Context base) {
    super(base);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        createChannel();
}

@TargetApi(Build.VERSION_CODES.O)
private void createChannel() {

    NotificationChannel nc = new NotificationChannel(CHANNEL_ID,CHANNEL_NAME,
            NotificationManager.IMPORTANCE_DEFAULT);

    nc.enableLights(false);
    nc.enableVibration(true);
    nc.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

    getManager().createNotificationChannel(nc);

}

public NotificationManager getManager() {

    if(notificationManager == null)

        notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    return notificationManager;

}

@TargetApi(Build.VERSION_CODES.O)
public Notification.Builder getDrinkShopNotification(String title,
                                                     String message,
                                                     Uri soundUri)
{

    return new Notification.Builder(getApplicationContext(),CHANNEL_ID)
            .setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setSound(soundUri)
            .setChannelId(CHANNEL_ID)
            .setAutoCancel(true);




   }


}
  <service
        android:name=".Services.MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
  implementation 'com.google.firebase:firebase-messaging:20.0.0'
  implementation 'com.google.firebase:firebase-core:17.2.1'
  implementation 'com.google.android.gms:play-services-auth:17.0.0'
 public interface IFCMService {

@Headers({
        "Content-Type:application/json",
        "Authorization:mytoken"
})
 @POST("fcm/send")
 Call<MyResponse> sendNotification(@Body DataMessage body);

}
===========================================舱单=======================================

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String channel_id = "the_id";


@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    Log.e("NEW_TOKEN",s);
    updateTokenToFirebase(s);

}


private void updateTokenToFirebase(String token) {
    IDrinkShopAPI mService = Common.getAPI();

    mService.updateToken("SERVER_01",token,"0")
            .enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    Log.d("DEBUG_TOKEN",response.body());
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    Log.d("DEBUG_TOKEN",t.getMessage());
                }
            });


}



@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    if(remoteMessage.getData() != null){

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            sendNotification26(remoteMessage);
        else
            sendNotification(remoteMessage);

    }

}

private void sendNotification26(RemoteMessage remoteMessage) {
    Map<String,String> data = remoteMessage.getData();

    String title = data.get("title");
    String message = data.get("message");



    NotificationHelper helper ;
    Notification.Builder builder;
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    helper = new NotificationHelper(this);

    builder = helper.getDrinkShopNotification(title,message,defaultSoundUri);

    helper.getManager().notify(new Random().nextInt(),builder.build());


}


private void sendNotification(RemoteMessage remoteMessage) {

    Map<String,String> data = remoteMessage.getData();

   String title = data.get("title");
   String message = data.get("message");



    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri);

    NotificationManager mn =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    mn.notify(new Random().nextInt(),builder.build());

   }
}
//this class is used to implement notification for all android versions

public class NotificationHelper extends ContextWrapper {

private static final String CHANNEL_ID = "the_id";
private static final String CHANNEL_NAME = "Drink_Shop";

private NotificationManager notificationManager;

public NotificationHelper(Context base) {
    super(base);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        createChannel();
}

@TargetApi(Build.VERSION_CODES.O)
private void createChannel() {

    NotificationChannel nc = new NotificationChannel(CHANNEL_ID,CHANNEL_NAME,
            NotificationManager.IMPORTANCE_DEFAULT);

    nc.enableLights(false);
    nc.enableVibration(true);
    nc.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

    getManager().createNotificationChannel(nc);

}

public NotificationManager getManager() {

    if(notificationManager == null)

        notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    return notificationManager;

}

@TargetApi(Build.VERSION_CODES.O)
public Notification.Builder getDrinkShopNotification(String title,
                                                     String message,
                                                     Uri soundUri)
{

    return new Notification.Builder(getApplicationContext(),CHANNEL_ID)
            .setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setSound(soundUri)
            .setChannelId(CHANNEL_ID)
            .setAutoCancel(true);




   }


}
  <service
        android:name=".Services.MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
  implementation 'com.google.firebase:firebase-messaging:20.0.0'
  implementation 'com.google.firebase:firebase-core:17.2.1'
  implementation 'com.google.android.gms:play-services-auth:17.0.0'
 public interface IFCMService {

@Headers({
        "Content-Type:application/json",
        "Authorization:mytoken"
})
 @POST("fcm/send")
 Call<MyResponse> sendNotification(@Body DataMessage body);

}
===========================================IFCMService=======================================

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String channel_id = "the_id";


@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    Log.e("NEW_TOKEN",s);
    updateTokenToFirebase(s);

}


private void updateTokenToFirebase(String token) {
    IDrinkShopAPI mService = Common.getAPI();

    mService.updateToken("SERVER_01",token,"0")
            .enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    Log.d("DEBUG_TOKEN",response.body());
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    Log.d("DEBUG_TOKEN",t.getMessage());
                }
            });


}



@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    if(remoteMessage.getData() != null){

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            sendNotification26(remoteMessage);
        else
            sendNotification(remoteMessage);

    }

}

private void sendNotification26(RemoteMessage remoteMessage) {
    Map<String,String> data = remoteMessage.getData();

    String title = data.get("title");
    String message = data.get("message");



    NotificationHelper helper ;
    Notification.Builder builder;
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    helper = new NotificationHelper(this);

    builder = helper.getDrinkShopNotification(title,message,defaultSoundUri);

    helper.getManager().notify(new Random().nextInt(),builder.build());


}


private void sendNotification(RemoteMessage remoteMessage) {

    Map<String,String> data = remoteMessage.getData();

   String title = data.get("title");
   String message = data.get("message");



    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri);

    NotificationManager mn =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    mn.notify(new Random().nextInt(),builder.build());

   }
}
//this class is used to implement notification for all android versions

public class NotificationHelper extends ContextWrapper {

private static final String CHANNEL_ID = "the_id";
private static final String CHANNEL_NAME = "Drink_Shop";

private NotificationManager notificationManager;

public NotificationHelper(Context base) {
    super(base);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        createChannel();
}

@TargetApi(Build.VERSION_CODES.O)
private void createChannel() {

    NotificationChannel nc = new NotificationChannel(CHANNEL_ID,CHANNEL_NAME,
            NotificationManager.IMPORTANCE_DEFAULT);

    nc.enableLights(false);
    nc.enableVibration(true);
    nc.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

    getManager().createNotificationChannel(nc);

}

public NotificationManager getManager() {

    if(notificationManager == null)

        notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    return notificationManager;

}

@TargetApi(Build.VERSION_CODES.O)
public Notification.Builder getDrinkShopNotification(String title,
                                                     String message,
                                                     Uri soundUri)
{

    return new Notification.Builder(getApplicationContext(),CHANNEL_ID)
            .setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setSound(soundUri)
            .setChannelId(CHANNEL_ID)
            .setAutoCancel(true);




   }


}
  <service
        android:name=".Services.MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
  implementation 'com.google.firebase:firebase-messaging:20.0.0'
  implementation 'com.google.firebase:firebase-core:17.2.1'
  implementation 'com.google.android.gms:play-services-auth:17.0.0'
 public interface IFCMService {

@Headers({
        "Content-Type:application/json",
        "Authorization:mytoken"
})
 @POST("fcm/send")
 Call<MyResponse> sendNotification(@Body DataMessage body);

}
公共接口IFCMService{
@标题({
“内容类型:应用程序/json”,
“授权:mytoken”
})
@邮政(“fcm/发送”)
调用sendNotification(@Body DataMessage Body);
}
========================================向服务器发送通知===============================

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String channel_id = "the_id";


@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    Log.e("NEW_TOKEN",s);
    updateTokenToFirebase(s);

}


private void updateTokenToFirebase(String token) {
    IDrinkShopAPI mService = Common.getAPI();

    mService.updateToken("SERVER_01",token,"0")
            .enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    Log.d("DEBUG_TOKEN",response.body());
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    Log.d("DEBUG_TOKEN",t.getMessage());
                }
            });


}



@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    if(remoteMessage.getData() != null){

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            sendNotification26(remoteMessage);
        else
            sendNotification(remoteMessage);

    }

}

private void sendNotification26(RemoteMessage remoteMessage) {
    Map<String,String> data = remoteMessage.getData();

    String title = data.get("title");
    String message = data.get("message");



    NotificationHelper helper ;
    Notification.Builder builder;
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    helper = new NotificationHelper(this);

    builder = helper.getDrinkShopNotification(title,message,defaultSoundUri);

    helper.getManager().notify(new Random().nextInt(),builder.build());


}


private void sendNotification(RemoteMessage remoteMessage) {

    Map<String,String> data = remoteMessage.getData();

   String title = data.get("title");
   String message = data.get("message");



    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri);

    NotificationManager mn =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    mn.notify(new Random().nextInt(),builder.build());

   }
}
//this class is used to implement notification for all android versions

public class NotificationHelper extends ContextWrapper {

private static final String CHANNEL_ID = "the_id";
private static final String CHANNEL_NAME = "Drink_Shop";

private NotificationManager notificationManager;

public NotificationHelper(Context base) {
    super(base);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        createChannel();
}

@TargetApi(Build.VERSION_CODES.O)
private void createChannel() {

    NotificationChannel nc = new NotificationChannel(CHANNEL_ID,CHANNEL_NAME,
            NotificationManager.IMPORTANCE_DEFAULT);

    nc.enableLights(false);
    nc.enableVibration(true);
    nc.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

    getManager().createNotificationChannel(nc);

}

public NotificationManager getManager() {

    if(notificationManager == null)

        notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    return notificationManager;

}

@TargetApi(Build.VERSION_CODES.O)
public Notification.Builder getDrinkShopNotification(String title,
                                                     String message,
                                                     Uri soundUri)
{

    return new Notification.Builder(getApplicationContext(),CHANNEL_ID)
            .setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setSound(soundUri)
            .setChannelId(CHANNEL_ID)
            .setAutoCancel(true);




   }


}
  <service
        android:name=".Services.MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
  implementation 'com.google.firebase:firebase-messaging:20.0.0'
  implementation 'com.google.firebase:firebase-core:17.2.1'
  implementation 'com.google.android.gms:play-services-auth:17.0.0'
 public interface IFCMService {

@Headers({
        "Content-Type:application/json",
        "Authorization:mytoken"
})
 @POST("fcm/send")
 Call<MyResponse> sendNotification(@Body DataMessage body);

}
//此方法用于将通知发送到服务器设备

  private void sendNotificationToServer(OrderResult orderResult) {

    mService.getToken("SERVER_01", "1")
            .enqueue(new Callback<Token>() {
                @Override
                public void onResponse(Call<Token> call, Response<Token> response) {

                    Map<String,String> contentSend = new HashMap<>();
                    contentSend.put("title","NEW ORDER");
                    contentSend.put("message","You have got new order" + orderResult.getOrderId());

                    DataMessage dataMessage = new DataMessage();
                    if(response.body().getToken() != null)
                        dataMessage.setTo(response.body().getToken());

                        dataMessage.setData(contentSend);

                        IFCMService ifcmService = Common.getFCMService();
                         ifcmService.sendNotification(dataMessage)
                            .enqueue(new Callback<MyResponse>() {
                                @Override
                                public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {

                                    if(response.code() == 200){

                                        if(response.body().success == 1){

                                     Toast.makeText(CartActivity.this,
                                     getResources().getString(R.string.order_submitted), Toast.LENGTH_SHORT)
                                        .show();
                                            //Clear Carts From Room Database
                                            Common.cartRepository.emptyCart();
                                            //finish();
                                        }
                                        else {

                                            Toast.makeText(CartActivity.this, "Send Notification Failed", Toast.LENGTH_SHORT).show();
                                        }
                                    }

                                }

                                @Override
                                public void onFailure(Call<MyResponse> call, Throwable t) {

                                    Toast.makeText(CartActivity.this, ""+t.getMessage(), Toast.LENGTH_SHORT).show();

                                }
                            });

                }

                @Override
                public void onFailure(Call<Token> call, Throwable t) {

                    Toast.makeText(CartActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });



 }
private void sendNotificationToServer(OrderResult OrderResult){
mService.getToken(“服务器01”、“1”)
.enqueue(新的回调函数(){
@凌驾
公共void onResponse(调用、响应){
Map contentSend=new HashMap();
contentSend.put(“标题”、“新订单”);
contentSend.put(“消息”,“您有新订单”+orderResult.getOrderId());
DataMessage DataMessage=新的DataMessage();
if(response.body().getToken()!=null)
setTo(response.body().getToken());
dataMessage.setData(contentSend);
IFCMService IFCMService=Common.getFCMService();
ifcmService.sendNotification(数据消息)
.enqueue(新的回调函数(){
@凌驾
公共void onResponse(调用、响应){
if(response.code()==200){
if(response.body().success==1){
Toast.makeText(CartActivity.this,
getResources().getString(R.string.order\u submitted),Toast.LENGTH\u SHORT)
.show();
//从房间数据库中清除购物车
Common.cartRepository.emptyCart();
//完成();
}
否则{
Toast.makeText(CartActivity.this,“发送通知失败”,Toast.LENGTH_SHORT.show();
}
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(CartActivity.this,“+t.getMessage(),Toast.LENGTH_SHORT.show();
}
});
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(CartActivity.this,t.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}

当应用程序在后台运行时,系统托盘中会收到通知,点击时,意图会发送到您的活动默认值,并带有其内容有效负载的通知

当您的应用程序在前端运行时,会通过FirebaseMessagingService和您覆盖的逻辑接收通知

我认为你应该添加第一点的逻辑,当应用程序在后台运行时

请点击这里了解更多信息


我通过将firebase消息传递库降级如下(实现'com.google.firebase:firebase消息传递:17.3.4')以及NotificationCompat中的.setColor(ContextCompat.getColor(this,R.color.colorAccent))解决了这个问题。然而,对于低于26的SDK版本,这解决了我的问题。有人知道为什么它还在使用高于26的API进行兑现吗?!请帮帮我

当应用程序在后台运行时,您是否尝试过接收通知?为什么会有什么不同,兄弟?!您也需要build()通知,但我在代码中看不到.build()。这里我使用helper类helper.getManager().notify(new Random().nextInt(),builder.build());正如你们所说,logcat中显示的是空消息,这意味着,你们正在接收它,但并没有正确地构建通知,并在推送它的同时正确地传递参数,当应用程序在后台运行时,尝试检查它。