Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Messaging Service Not Working On Older Android

Tags:

java

android

Recently I`ve migrated GCM to FCM and after some struggle, I've managed to make everything work with the help of this great community.

Now when I tested notifications on older version of Android ( Nougat) it doesn't work, app just crash, I've found out that its something related to versions as older ones doesn't support channel managers.

I've found few solutions on StackOverflow but I haven't find a suitable one for my problem so I was hoping someone could toss me a hint or solution.

I appreciate all the answers and help.

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

}

like image 475
DevGuy Avatar asked Nov 06 '22 10:11

DevGuy


1 Answers

I think it is crashing because Notification Channels dose not supported in android versions older than Oreo. So you can fix it by adding an android sdk version checker and set notification channel just when app is running on android Oreo or higher:

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

Also notice that I used NotificationCompat.Builder instead of Notification.Builder and removed .setChannel() because it is not necessary when we are passing the channel id in builder constructor.

like image 117
Masoud Maleki Avatar answered Nov 14 '22 22:11

Masoud Maleki