0

I need your help , i went to the google and searched alot,I am facing serious issue when my app is in background and i send the notification from the backened then the notification is coming but it not load the data when i make my app from background to forground,if i touch the notification icon then i am getting the data.

I went to this link also How to handle notification when app in background in Firebase

There guys are telling you have to click the notification icon to load a data , what is an alternative of FCM.

I tried :

     @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.v(TAG, "From: " + remoteMessage.getFrom());
        // Check if message contains a data payload.
//        if (remoteMessage.getData().size() > 0) {
//            Log.v(TAG, "Message data payload: " + remoteMessage.getData().get("order"));
//
//        }
//        if (remoteMessage.getNotification() != null) {
//            Log.v(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
//
//        }
        sendNotification(remoteMessage.getData().get("order"));
    }
    private void sendNotification(String messageBody) {

        Log.e("inside sendNotification","sendNotification");
        JSONObject jobj_order= null;
        try {
            dbHelper=new DBHelper(this);
            jobj_order = new JSONObject(messageBody);
            dbHelper.insertOrder(jobj_order.getJSONObject("supplier").toString(),jobj_order.getJSONObject("request").toString(),jobj_order.getString("expiry"),jobj_order.getString("id"),jobj_order.getString("quantity"));
            Log.e("inside try block","inside try block");

            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction(MainActivity.NOTIFY_ACTIVITY_ACTION );
            broadcastIntent.putExtra("reload", true);
            sendBroadcast(broadcastIntent);
        } catch (JSONException e) {
            e.printStackTrace();
        }




        MainActivity.isNotification=true;

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.splash_logo)
                .setContentTitle("Logizo")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
Community
  • 1
  • 1
Barnali Bhattacharjee
  • 497
  • 2
  • 10
  • 24

1 Answers1

1

It is easier to use the 'data message' I think you are using 'notification'. The data message always load the class onMessageReceived.

Below is my working code:

 @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"));
}

private void sendNotification(String messageTitle,String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT);

    long[] pattern = {500,500,500,500,500};

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

    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setVibrate(pattern)
            .setLights(Color.BLUE,1,1)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

If this isn't working please post your onReciveMessage code implementation.

Bhagwat K
  • 2,982
  • 2
  • 23
  • 33
  • Change your sendNotification(remoteMessage.getNotification().getBody()); to sendNotification(remoteMessage.getData()) and whatever data your getting parse that data in sendNotification() method.. – Bhagwat K Nov 20 '16 at 05:52
  • Just try to print remoteMessage.getData().get("order"), this should not have to be null,if this is null then your notification will not get created. also check NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); this code is getting called or not. – Bhagwat K Nov 20 '16 at 06:52
  • it is not getting called when my app is minimised (background).notification icon is coming , but when i maximize my app the data is not coming.i updated the fcm in gradle to 9.8.0 ,still same problem – Barnali Bhattacharjee Nov 20 '16 at 06:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128557/discussion-between-prashant-and-barnali-bhattacharjee). – Bhagwat K Nov 20 '16 at 07:32