Firebase 消息服务不适用于旧版 Android

Dev*_*Guy 5 java android

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

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

我在 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);
}
Run Code Online (Sandbox Code Playgroud)

}

Mas*_*eki 1

我认为它崩溃了,因为早于 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);
Run Code Online (Sandbox Code Playgroud)

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