0

I was following this tutorial and I'm using FCM to send push notifications, but for some reason the code seems to not work. In onMessageRecieved() im building the notification, making the phone vibrate and show the notification, and for some reason the phone vibrates, but no notification is shown. This is the code:

Intent intent=new Intent(this, WelcomeActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent=PendingIntent.getActivity(this
                    ,0
                    ,intent
                    ,PendingIntent.FLAG_ONE_SHOT);
            NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
            notificationBuilder.setContentTitle(getString(R.string.app_name));
            notificationBuilder.setContentText(message);
            Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            notificationBuilder.setSound(soundUri);
            notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
            notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher));
            notificationBuilder.setAutoCancel(true);
            Vibrator v=(Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(1000);
            notificationBuilder.setContentIntent(pendingIntent);
            NotificationManager notificationManager=(NotificationManager)
                    getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0,notificationBuilder.build());

If anyone could tell me what's wrong it'd be awesome.. Thanks :)

KENdi
  • 7,205
  • 2
  • 14
  • 26
adi harel
  • 17
  • 5
  • https://stackoverflow.com/questions/43093260/notification-not-showing-in-oreo – Bek Mar 14 '19 at 09:05
  • Possible duplicate of [Notification not showing in Oreo](https://stackoverflow.com/questions/43093260/notification-not-showing-in-oreo) – zeropublix Mar 14 '19 at 09:17

2 Answers2

0

From your code and the fact that the notification doesn't appear, I assume you are testing on a device with Android version above 8.0.

For Android 8.0 (Oreo) and above, you need to use notification channels, in order to display the notification. Refer to this link for more details.

deluxe1
  • 567
  • 2
  • 11
  • Will this notification channels method work on Android 8.0 and below too? Or do I need to check the version of the phone and choose which method to choose? – adi harel Mar 14 '19 at 09:06
  • You only need to use notification channels for Android 8.0 and above. So, yes, check for the Android version and only use channels for 8.0 and above. – deluxe1 Mar 14 '19 at 09:08
0

For Android 8 and above use Notification channel like below

Intent intent=new Intent(this, WelcomeActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent=PendingIntent.getActivity(this
                    ,0
                    ,intent
                    ,PendingIntent.FLAG_ONE_SHOT);
            NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
            notificationBuilder.setContentTitle(getString(R.string.app_name));
            notificationBuilder.setContentText(message);
            Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            notificationBuilder.setSound(soundUri);
            notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
            notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher));
            notificationBuilder.setAutoCancel(true);
            Vibrator v=(Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(1000);
            notificationBuilder.setContentIntent(pendingIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
           /* NotificationChannel channel = new NotificationChannel(channelId,
                    "Membership Alerts",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);*/

            CharSequence name = getString(R.string.notification_channel_name);
            String description = getString(R.string.notification_channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(channelId, name, importance);
            channel.setDescription(description);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
Abid Khan
  • 2,231
  • 3
  • 17
  • 44