Firebase消息服务无法在较旧的Android上运行 - java

最近,我已将GCM迁移到FCM,经过一番努力,在这个强大的社区的帮助下,我设法使一切正常。

现在,当我在较旧版本的Android(Nougat)上测试通知时,它不起作用,应用程序崩溃了,我发现它与版本相关,因为较旧的版本不支持渠道管理器。

我在StackOverflow上发现了很少的解决方案,但是找不到适合我的问题的解决方案,因此我希望有人可以向我提出提示或解决方案。

我感谢所有的答案和帮助。

public class MessagingService extends FirebaseMessagingService {

private static final String TAG = "FCM Message";

public MessagingService() {
    super();
}



@TargetApi(Build.VERSION_CODES.O)
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    String message = remoteMessage.getData().get("team");



    Intent intent=new Intent(getApplicationContext(),MainActivity.class);
    String CHANNEL_ID="channel";
    Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID,"channel",NotificationManager.IMPORTANCE_HIGH);
    PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,0);
    Notification notification=new Notification.Builder(getApplicationContext(),CHANNEL_ID)
            .setContentText(message)
            //  .setContentTitle(title)
            .setSound(defaultSound)
            .setContentIntent(pendingIntent)
            .setChannelId(CHANNEL_ID)
            .setSmallIcon(android.R.drawable.sym_action_chat)
            .setWhen(System.currentTimeMillis())
            .setPriority(Notification.PRIORITY_MAX)
            .build();

    NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(notificationChannel);
    notificationManager.notify(1,notification);
}

}

参考方案

我认为它是崩溃的,因为早于Oreo的android版本不支持通知频道。
因此,您可以通过添加android sdk版本检查器进行修复,并仅在应用程序在android Oreo或更高版本上运行时设置通知通道:

    Intent intent=new Intent(getApplicationContext(),MainActivity.class);
    Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(), 1, intent, 0);
    Notification notification=new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
            .setContentText(message)
            .setSound(defaultSound)
            .setContentIntent(pendingIntent)
            .setSmallIcon(android.R.drawable.sym_action_chat)
            .setWhen(System.currentTimeMillis())
            .setPriority(Notification.PRIORITY_MAX)
            .build();

    NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String CHANNEL_ID="channel";
        NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID,"channel",NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(notificationChannel);
    }
    notificationManager.notify(1, notification);

还要注意,我使用NotificationCompat.Builder而不是Notification.Builder并删除了.setChannel(),因为在构建器构造函数中传递通道ID时没有必要。

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

Java:BigInteger,如何通过OutputStream编写它 - java

我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…

Java-如何将此字符串转换为日期? - java

我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…

Java:从类中查找项目名称 - java

仅通过类的实例,如何使用Java反射或类似方法查找项目名称?如果不是,项目名称(我真正想要的是)可以找到程序包名称吗? 参考方案 项目只是IDE使用的简单组织工具,因此项目名称不是类或JVM中包含的信息。要获取软件包,请使用Class#getPackage()。然后,可以调用Package#getName()将包作为您在代码的包声明中看到的String来获取…

JAVA 8具有任何匹配属性的对象的过滤器列表 - java

我的要求是通过匹配任何属性的字符串来过滤对象列表。例如,假设Contact类具有三个属性:街道,城市,电话。我知道java流过滤器是如何工作的,在这里我必须将输入字符串与每个属性进行比较,如下所示:contactList.stream().filter(contact -> contact.getStreet().equals("dubai&…