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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With