2

I have configured the FCM for my 3° application android but in this case the notification is received only if application is closed or background, when app is running nothing appears.

This is my .FirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());    

    }    

    private void sendNotification(String title, String body) {

        Random random = new Random();
        int m = random.nextInt(9999 - 1000) + 1000;

        Intent showFullQuoteIntent = new Intent(this, Home.class);
        showFullQuoteIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        // both of these approaches now work: FLAG_CANCEL, FLAG_UPDATE; the uniqueInt may be the real solution.
        //PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueInt, showFullQuoteIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueInt, showFullQuoteIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Notification notification = new NotificationCompat.Builder(getApplicationContext(), "")
                .setSmallIcon(R.drawable.logo)
                .setContentTitle(title)
                .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.logo))
                .setColor(getResources().getColor(R.color.colorPrimary))
                .setAutoCancel(false)
                .setSound(notificationSound)
                .setContentIntent(pendingIntent)
                .setContentText(body)
                .build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(m, notification);

    }
}

Called in:

 <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

Please can you help me? FCM is linked to my firebase account and i send notification from firebase site...

FCM Log:

D/FA: Logging event (FE): notification_foreground(_nf), Bundle[{firebase_event_origin(_o)=fcm, firebase_screen_class(_sc)=Home, firebase_screen_id(_si)=-8508869816157962301, message_device_time(_ndt)=0, message_name(_nmn)=test, message_time(_nmt)=1521054559, message_id(_nmid)=5215133478455582605}]

I have use some code like: Notification not showing in Android 8 Oreo And Guidelines from Google: https://developer.android.com/training/notify-user/build-notification.html

But if application is opened notification not received but logged in console..

Marcus J.Kennedy
  • 680
  • 5
  • 22

1 Answers1

1

I have solved with this code:

private void sendNotification(String title, String body) {

        Random random = new Random();
        int m = random.nextInt(9999 - 1000) + 1000;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String CHANNEL_ID = "my_channesl_01";
            CharSequence name = "NEWS";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationManager mNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel mChannel = null;
            mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            mNotificationManager.createNotificationChannel(mChannel);
            Intent showFullQuoteIntent = new Intent(this, Home.class);
            showFullQuoteIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

            int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueInt, showFullQuoteIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            Notification notification = new NotificationCompat.Builder(this.getApplicationContext(), "")
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle(title)
                    .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.logo))
                    .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
                    .setAutoCancel(false)
                    .setSound(notificationSound)
                    .setContentIntent(pendingIntent)
                    .setContentText(body)
                    .setChannelId(CHANNEL_ID)
                    .build();

            mNotificationManager.notify(m, notification);
        }
        else
        {
            Intent showFullQuoteIntent = new Intent(this, Home.class);
            showFullQuoteIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueInt, showFullQuoteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            Notification notification = new NotificationCompat.Builder(getApplicationContext(), "")
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle(title)
                    .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.logo))
                    .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
                    .setAutoCancel(false)
                    .setSound(notificationSound)
                    .setContentIntent(pendingIntent)
                    .setContentText(body)
                    .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(m, notification);
        }
    }
Marcus J.Kennedy
  • 680
  • 5
  • 22