0

Class FirebaseMessageReceiver

  public class FirebaseMessageReceiver extends FirebaseMessagingService {
        public static final String FCM_PARAM = "picture";
        private static final String CHANNEL_NAME = "FCM";
        private static final String CHANNEL_DESC = "Firebase Cloud Messaging";
        private int numMessages = 0;
        Bitmap bitmap;
    
        @Inject
        AppPreference appPreference;
    
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            AndroidInjection.inject(this);
            super.onMessageReceived(remoteMessage);
            RemoteMessage.Notification notification = remoteMessage.getNotification();
            Map<String, String> data = remoteMessage.getData();
            Log.d("FromNotification", remoteMessage.getFrom());
            sendNotificaionBroadcast();
            if (remoteMessage.getNotification().getImageUrl() != null) {
                String imageUri = remoteMessage.getNotification().getImageUrl().toString();
                bitmap = getBitmapFromUrl(imageUri);
                sendNotification(notification, data, bitmap);
            } else {
                sendNotification(notification, data, null);
    
            }
        }
    
        private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data, Bitmap bitmap) {
            Bundle bundle = new Bundle();
            bundle.putString(FCM_PARAM, data.get(FCM_PARAM));
            appPreference.setNewNotification(true);
            Intent intent = new Intent(this, SplashScreenActivity1.class);
            intent.putExtras(bundle);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, getString(R.string.notification_channel_id))
                    .setContentTitle(notification.getTitle())
                    .setContentText(notification.getBody())
                    .setAutoCancel(true)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    //.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.win))
                    .setContentIntent(pendingIntent)
                    .setContentInfo("RoveDashCam")
                    .setLargeIcon(bitmap)
                    .setColor(Color.parseColor("#80FF8C00"))
                    .setLights(Color.RED, 1000, 300)
                    .setDefaults(Notification.DEFAULT_VIBRATE)
                    .setNumber(++numMessages)
                    .setSmallIcon(R.drawable.ic_notification);
    
            try {
                String picture = data.get(FCM_PARAM);
                if (picture != null && !"".equals(picture)) {
                    URL url = new URL(picture);
                    Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                    notificationBuilder.setStyle(
                            new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
                    );
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(
                        getString(R.string.notification_channel_id), CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT
                );
                channel.setDescription(CHANNEL_DESC);
                channel.setShowBadge(true);
                channel.canShowBadge();
                channel.enableLights(true);
                channel.setLightColor(Color.RED);
                channel.enableVibration(true);
                channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500});
                assert notificationManager != null;
                notificationManager.createNotificationChannel(channel);
            }
            assert notificationManager != null;
            notificationManager.notify(0, notificationBuilder.build());
        }
    
        public Bitmap getBitmapFromUrl(String imageUrl) {
            try {
                URL url = new URL(imageUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                return BitmapFactory.decodeStream(input);
    
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
    
            }
        }
    
        private void sendNotificaionBroadcast() {
            Intent intent = new Intent("IntentFilterAction");
            intent.putExtra("MyDataKey", "myData");
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    
        }
    }

This is manifest code

<meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/notification_channel_id" />

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_notification" />

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/white" />

        <service
            android:name="com.rovedashcam.newmodeule.base.commondots.FirebaseMessageReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

When App is no foreground everything works fine i am getting customize notification in notification manager. and i am able to redirect to activity which i have defined in above code .

But problem is coming when app is background when app is in background i am getting notification in notification manager but we are getting some different color code an all i mean not getting customise which we defined in code . and also when i tap on that notification i am unable to launch or open activity so that i can do some operation even i am not getting call back in FirebaseMessageReceiver class . can any one please help me what i am doing wrong what i need to changes.

Thanks

MARSH
  • 117
  • 1
  • 7
  • 22
  • https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase Check this – Sagar May 24 '23 at 08:09
  • Thanks for posting but its talk about that we should get call back in onMessageReceived even in background or kill also right but i am not getting call back when app is killed i am getting only when foreground . – MARSH May 24 '23 at 08:52

0 Answers0