Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 仅适用于api 26的fcm通知崩溃_Java_Android - Fatal编程技术网

Java 仅适用于api 26的fcm通知崩溃

Java 仅适用于api 26的fcm通知崩溃,java,android,Java,Android,Android上接收到的Android fcm通知的API低于26,但API26(Oreo 8.0)没有,这会导致应用程序崩溃 ============================MyFirebaseMessagingService=============================== public class MyFirebaseMessagingService extends FirebaseMessagingService { private static f

Android上接收到的Android fcm通知的API低于26,但API26(Oreo 8.0)没有,这会导致应用程序崩溃

============================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)
        .setColor(ContextCompat.getColor(this, R.color.colorAccent))
        .setSound(defaultSoundUri);

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

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

    }
  }
   <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)
        .setColor(ContextCompat.getColor(this, R.color.colorAccent))
        .setSound(defaultSoundUri);

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

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

    }
  }
   <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)
        .setColor(ContextCompat.getColor(this, R.color.colorAccent))
        .setSound(defaultSoundUri);

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

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

    }
  }
   <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)
        .setColor(ContextCompat.getColor(this, R.color.colorAccent))
        .setSound(defaultSoundUri);

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

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

    }
  }
   <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();
}
});
}

我认为您使代码变得不必要的复杂

试试下面的代码

private void createNotification(String title, String message) {

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

    // only create notification channel if SDK >= 26
    if (android.os.Build.VERSION.SDK_INT >= 26) {
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);

        channel.enableLights(false);
        channel.enableVibration(true);
        channel.setDescription(CHANNEL_DESC);

        manager.createNotificationChannel(channel);
    }

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(android.R.drawable.stat_notify_more)
            .setContentTitle(title)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setSound(defaultSoundUri)
            .setColor(ContextCompat.getColor(this, R.color.colorAccent))
            .setContentText(message);


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

}

你可以添加更多关于你正在获取的错误的信息吗?你忘了发布崩溃的stacktrace。请共享错误日志。我刚刚注意到应用程序只在API 26(Oreo 8.0)上崩溃。stacktrace没有显示任何错误消息,但应用程序崩溃。应用程序显示消息“system UI has stopped”(系统UI已停止),但Logcat中没有显示任何内容!我已经通过将通知图标资源从mipmap更改为drawable解决了这个问题。无论如何,请尝试一下这段代码。您的代码对于通知来说太复杂了