Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify firebase push notification in android when app is in background

I am only receiving push notification when app is in background, I couldn't find what exactly triggered when my app receive push . I just want to change the notification body , as an example if the notification message is "hi" I want to show user "hi user".

public class MyFcmListenerService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage message) {
        //nothing triggered here when app is in background
    }

}
like image 660
mSapps Avatar asked Jan 20 '26 01:01

mSapps


2 Answers

You can, you only need to know how firebase push notifications work in android.

you need to override

handleIntent function.

This function handle Firebase notifications in background. So inside it you will make your push notification taking all the data sent in push message. Don't forget extract information from message. You can use default spaces like title or body but also you can send some custom data.

Next I will attach a example code how it works.

Note: if you haven't this method then you need to upgrade firebase version up than 10.0.1

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    }

    @Override
    public void handleIntent(Intent intent) {
        //super.handleIntent(intent);

        Log.d(TAG,"Handling Intent");
        Bundle mBundle = intent.getExtras();
        String img = mBundle.getString("imgURL");
        String title = mBundle.getString("gcm.notification.title");
        String body = mBundle.getString("gcm.notification.body");
        mBundle.putInt("promoId",Integer.valueOf(mBundle.getString("promoId")));
        Integer id = mBundle.getInt("promoId");

        sendNotification(mBundle);
    }

    private void sendNotification(Bundle mBundle) {
        // Create an explicit content Intent that starts the main Activity.
        Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);

        notificationIntent.putExtras(mBundle);

        // Construct a task stack.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

        // Add the main Activity to the task stack as the parent.
        stackBuilder.addParentStack(MainActivity.class);

        // Push the content Intent onto the stack.
        stackBuilder.addNextIntent(notificationIntent);

        // Get a PendingIntent containing the entire back stack.
        PendingIntent notificationPendingIntent =
                stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        // Get a notification builder that's compatible with platform versions >= 4
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        String title = mBundle.getString("gcm.notification.title");
        String body = mBundle.getString("gcm.notification.body");

        // Define the notification settings.
        builder.setSmallIcon(R.mipmap.ic_launcher)
                // In a real app, you may want to use a library like Volley
                // to decode the Bitmap.
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                        R.mipmap.ic_launcher))
                .setColor(Color.RED)
                .setContentTitle(title)
                .setContentText(body)
                .setContentIntent(notificationPendingIntent);

        // Dismiss notification once the user touches it.
        builder.setAutoCancel(true);

        // Get an instance of the Notification manager
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Issue the notification
        mNotificationManager.notify(0, builder.build());
    }

}

if you have any question ask and I will edit mi answer.

like image 163
Daniel Jose Padilla Peña Avatar answered Jan 22 '26 15:01

Daniel Jose Padilla Peña


$fields = array(
'registration_ids' => $reg_id ,
'priority' => "high",
'data' => array(******));    
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

I have faced the same problem. Removed notification key values from my PHP firebase Service. My issues got resolved. I am just using registration_ids, priority, data

like image 45
Raja Peela Avatar answered Jan 22 '26 13:01

Raja Peela