I encountered a strange problem today, push notification is not showing in samsung devices which have oreo in it but it's working fine in other oreo devices. Here is the code i am using:
private void sendNotification(String messageTitle,String messageBody) {
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder=null;
try {
Intent intent = new Intent(this, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
String NOTIFICATION_CHANNEL="NB Shop Notification";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
.setSound(defaultSoundUri)
.setChannelId(NOTIFICATION_CHANNEL)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/* Create or update. */
NotificationChannel channel;
channel=new NotificationChannel(NOTIFICATION_CHANNEL,
NOTIFICATION_CHANNEL,
NotificationManager.IMPORTANCE_DEFAULT);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build());
}catch (Exception e)
{
Log.d("sendNotification",e.toString());
}
}
It does not showing any error in log.
I found the issue which causing the problem, the constructor(NotificationCompat.Builder(this)
) of NotificationCompat.Builder which has only single parameter is deprecated,
To get rid of deprecation we have to send channel id with constructor, So i have changed my code and passed channel id to the constructor like this:
NotificationCompat.Builder(this,NOTIFICATION_CHANNEL)
also removed
.setChannelId(NOTIFICATION_CHANNEL)
and now this code is also working on samsung devices.