3

I try to receive notification in android Oreo. But App does not receive any

notification. I also create notification Chanel but it's not work

If I send a notification from fcm then app received. but using the token app not received any notification. In other lower, version notification work proper. In Oreo it does not work.

Here is my MyFirebaseMessagingService class:

     public class MyFirebaseMessagingService extends FirebaseMessagingService {
     public static int NOTIFICATION_ID = 1;
     public static final String NOTIF_CHANNEL_ID = "my_channel_01";


     @Override
     public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        sendNotification(remoteMessage.getData());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotifChannel(this);
        }

     }

     @RequiresApi(api = Build.VERSION_CODES.O)
     private void createNotifChannel(MyFirebaseMessagingService 
     myFirebaseMessagingService)
     {
        NotificationChannel channel = new NotificationChannel(NOTIF_CHANNEL_ID,
                "MyApp events", NotificationManager.IMPORTANCE_LOW);
        // Configure the notification channel
        channel.setDescription("MyApp event controls");

        channel.setShowBadge(false);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

        NotificationManager manager = getApplicationContext().
        getSystemService(NotificationManager.class);

        manager.createNotificationChannel(channel);
        Log.d(TAG, "createNotifChannel: created=" + NOTIF_CHANNEL_ID);
        }

        private void sendNotification(Map<String, String> data) {

        int num = ++NOTIFICATION_ID;
        Bundle msg = new Bundle();
        for (String key : data.keySet()) {
            Log.e(key, data.get(key));
            msg.putString(key, data.get(key));
        }
        Intent intent = new Intent(this, HomeActivity.class);
        if (msg.containsKey("action")) {
            intent.putExtra("action", msg.getString("action"));
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, num /* 
        Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = 
        RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new 
         NotificationCompat.Builder(this)
                .setSmallIcon(instauser.application.apps.R.drawable.icon)
                .setContentTitle(msg.getString("title"))
                .setContentText(msg.getString("msg"))
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(num, notificationBuilder.build());
    }

} 

I also create a notification channel but it's not work

Mahesh Dangar
  • 806
  • 5
  • 11
Darshi
  • 201
  • 1
  • 3
  • 10
  • `onMessageReceived` is calling? If you are not getting your token then you can make intent service and by using that you can get your fcm token. I can share my snippet which is successfully run in every devices – Piyush Sep 28 '18 at 10:50

1 Answers1

2

You created notification channel, but didn't set it to notification

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        ...
        .setChannelId(NOTIF_CHANNEL_ID)
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98