I\'m trying to create a notification media controller in my app using the code below which is working fine on all devices expect Huawei P8 Lite with An
I had the same issue and the root cause was not able to setStyle, it was only giving this exception in Huawei P8 Lite, not on other devices.
So, what i had to do was check if current device is android version 5.0 and its manufacturer is Huawei and remove the setStyle property. please check below code
boolean isLollipopHuawei = (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1 ||
android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) && Build.MANUFACTURER.equalsIgnoreCase("HUAWEI");
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1 || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
if (isLollipopHuawei) {
return builder
.addAction(generateAction(android.R.drawable.ic_media_previous, "Previous", Constants.ACTION_PREVIOUS))
.addAction(action)
.addAction(generateAction(android.R.drawable.ic_media_next, "Next", Constants.ACTION_NEXT))
.setSmallIcon(R.mipmap.vpicon_grayscale)
.setContentTitle(getSongDataHelper().getTitle())
.setContentIntent(pendingIntent)
.setContentText(getSongDataHelper().getAlbum())
.setLargeIcon(getSongDataHelper().getAlbumArt())
//.setColor(color)
/* .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2)
.setMediaSession(mMediaSession.getSessionToken()))*/
.build();
} else {
return builder
.addAction(generateAction(android.R.drawable.ic_media_previous, "Previous", Constants.ACTION_PREVIOUS))
.addAction(action)
.addAction(generateAction(android.R.drawable.ic_media_next, "Next", Constants.ACTION_NEXT))
.setSmallIcon(R.mipmap.vpicon_grayscale)
.setContentTitle(getSongDataHelper().getTitle())
.setContentIntent(pendingIntent)
.setContentText(getSongDataHelper().getAlbum())
.setLargeIcon(getSongDataHelper().getAlbumArt())
//.setColor(color)
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2)
.setMediaSession(mMediaSession.getSessionToken()))
.build();
}
}
For some reason, Huawei devices with Android 5.0 crashes when using .setStyle()
method so you have two possibilities :
1 - detect device Manufacture if is Huawei or not, and have an Android 5.0 or below or not
public boolean isLolliPopHuawei() {
return (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1 ||
android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) && Build.MANUFACTURER.equalsIgnoreCase("HUAWEI");
}
2 - Use PlayerNotificationManager
instead
void exoPlayerNotification(Context context, SimpleExoPlayer exoPlayer, String title) {
String titlesonge;
String artist;
try {
titlesonge = StringUtils.substringBefore(title, " - ");
artist = StringUtils.substringAfter(title, " - ");
} catch (Exception e) {
titlesonge = title.substring(0, title.indexOf(" - "));
artist = title.substring(title.lastIndexOf(" - ") - 1);
}
String finalArtist = artist;
String finalTitlesonge = titlesonge;
mPlayerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
context,
"PRIMARY_CHANNEL_ID",
R.string.plaza,
NOTIFICATION_ID,
new PlayerNotificationManager.MediaDescriptionAdapter() {
@Override
public String getCurrentContentTitle(Player player) {
return finalArtist;
}
@Nullable
@Override
public PendingIntent createCurrentContentIntent(Player player) {
Intent intent = new Intent(service, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return PendingIntent.getActivity(service, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
@Override
public String getCurrentContentText(Player player) {
return finalTitlesonge;
}
@Nullable
@Override
public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) {
return BitmapFactory.decodeResource(service.getResources(), R.drawable.largeicon);
}
@Nullable
@Override
public String getCurrentSubText(Player player) {
return null;
}
}
);
mPlayerNotificationManager.setUseNavigationActions(false);
mPlayerNotificationManager.setFastForwardIncrementMs(0);
mPlayerNotificationManager.setRewindIncrementMs(0);
mPlayerNotificationManager.setColorized(true);
mPlayerNotificationManager.setColor(0xFFEEEEEE);
mPlayerNotificationManager.setUseChronometer(true);
mPlayerNotificationManager.setOngoing(true);
mPlayerNotificationManager.setPriority(NotificationCompat.PRIORITY_MAX);
mPlayerNotificationManager.setUsePlayPauseActions(true);
mPlayerNotificationManager.setSmallIcon(R.drawable.smallwidth);
mPlayerNotificationManager.setNotificationListener(new PlayerNotificationManager.NotificationListener() {
@Override
public void onNotificationStarted(int notificationId, Notification notification) {
service.startForeground(notificationId, notification);
}
@Override
public void onNotificationCancelled(int notificationId) {
service.stopSelf();
cancelNotify();
}
});
mPlayerNotificationManager.setPlayer(exoPlayer);
}
Since some Huawei devices don't support MediaStyle you need to build notification without styling. I experienced this issue on this models Huawei P8 Lite and Huawei Y3II. So, check if device is huawei and SDK versions as mentioned and create a simple notification as below. This question helped me to find solution Strange allow/deny question on Huawei 5.1 phone when showing notification . Anyway, I hope helps someone
boolean isLollipopHuawei = (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1 ||
android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) && Build.MANUFACTURER.equalsIgnoreCase("HUAWEI");
if (isLollipopHuawei) {
builder
.setContentTitle(description.getTitle())
.setContentText(contentText)
.setOngoing(true)
.setContentIntent(createContentIntent())
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.drawable.mobi_plc))
.addAction(R.drawable.ic_previous_outline_notification,
this.service.getString(R.string.next_station),
MediaButtonReceiver.buildMediaButtonPendingIntent(
this.service,
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS))
.addAction(R.drawable.ic_next_outline_notification,
this.service.getString(R.string.next_station),
MediaButtonReceiver.buildMediaButtonPendingIntent(
this.service,
PlaybackStateCompat.ACTION_SKIP_TO_NEXT))
.setSmallIcon(R.drawable.ic_stat)
.setAutoCancel(false);
}