3

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
    }

}
KENdi
  • 7,576
  • 2
  • 16
  • 31
mSapps
  • 601
  • 9
  • 24
  • In background it will trigger directly, you can't change the message. You need set message in your notification payload from server side. You can handle the payload using intent bundle from you mainactivity or action acitivity when app is in background. – Jd Prajapati Apr 06 '17 at 11:56
  • check it out [How to handle notification when app in background in Firebase](http://stackoverflow.com/a/37845174/3536264) – Hitesh Gehlot Apr 06 '17 at 12:09
  • From where you are sending the notification? I mean from `Firebase` console or your server? If you are sending from Firebase, while sending notification, goto advanced options and add data to the notification. And if you are sending from server, dont send display-messages (send only data-messages). – Faraz Apr 06 '17 at 12:19

2 Answers2

2

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.

1
$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

Raja Peela
  • 1,366
  • 2
  • 14
  • 26