0

Here is my code of notification. Actually, I'm getting the notification perfectly but when I click on the notification in a phone the app will open directly (when the app is in the background or killed). When the app is in the foreground, the ResultActivity will open correctly when I click on notification but the data is not showing over there.

Please check my code and let me know what I did wrong.

FirebaseMessagingService.java:

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

Map<String, String> data;
String Title, Message, PushType;
String body, title;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.e(TAG, "From: " + remoteMessage.getFrom() + "");
    data = remoteMessage.getData();
    JSONObject jsonObject = new JSONObject(data);

    try {
        Log.e("data", jsonObject.toString());
        body = jsonObject.getString("body");
        title = jsonObject.getString("title");
        Log.e("body", body);
        Log.e("title", title);
    } catch (JSONException e) {
        Log.e("exception>>>", e.toString() + "");
        e.printStackTrace();
        body = remoteMessage.getNotification().getBody();
        Log.e("body>>>", body + "");
    }

    sendNotification(body, title);
}



private void sendNotification(String message, String title){

    Log.e("message>>>", message + "");

    Intent intent = new Intent(this, ResultActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("body",message);
    intent.putExtra("title",title);

    int requestCode = 0;
    PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.fc)
            //.setSmallIcon(getNotificationIcon())
            .setContentText(message)
            .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
            .setAutoCancel(true)
            .setSound(sound)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, noBuilder.build()); //0 = ID of notification

}

ResultActivity.java:

public class ResultActivity extends Activity {
    private TextView Title,Message ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_view);
        Title=(TextView)findViewById(R.id.title);
        Message=(TextView)findViewById(R.id.message);

        if(getIntent().getExtras()!=null)
        {
            for(String key : getIntent().getExtras().keySet())
            {
                if(key.equals("title"))
                    Title.setText(getIntent().getExtras().getString(key));
      else if(key.equals("message"))
                Message.setText(getIntent().getExtras().getString(key));

            }

        }

    }
}

activity_notification_view.xml:

<TextView
    android:layout_marginTop="50dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/title"
    android:hint="title"
    android:gravity="center" />


<TextView
    android:hint="message"
    android:layout_marginTop="20dp"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/message" />

KENdi
  • 7,576
  • 2
  • 16
  • 31

2 Answers2

0
Intent intent = new Intent(this, ResultActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("body",message);
intent.putExtra("title",title);

//in activity add

String  body =getIntent().getStringExtra("body");
String  title =getIntent().getStringExtra("title"); 
  • ohh yaa it works, if my app is in foreground, but when my app is in background or killed the issue remain same that when i click on notification directly the app will open instead of result activity . – PRANEET NIRANJAN Jul 17 '18 at 09:57
  • PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode,intent, PendingIntent.FLAG_UPDATE_CURRENT); – Siddhesh Golatkar Jul 17 '18 at 10:08
0

You are using different key in putExtra and in getExtra.

In above code you are passing data in body and title key and you are trying to find the data on title and message.

Use same key for for getter and setter.

Jarvis
  • 1,714
  • 2
  • 20
  • 35
  • yup thanks a lot ,but it works when app is in foreground, if app is in background or killed the issue remain same that when i click on notification directly app will open instead of result activity ...? – PRANEET NIRANJAN Jul 17 '18 at 09:53
  • with type of FCM message type you are sending? Data message or Notification message ? – Jarvis Jul 17 '18 at 12:46
  • i think i m using notification message after my Research, can you tell me how to send data message using firebase.?? – PRANEET NIRANJAN Jul 18 '18 at 07:35
  • https://stackoverflow.com/questions/40726030/unable-to-send-data-message-using-firebase-console – Jarvis Jul 20 '18 at 12:39
  • you have to add data as key value pair, check the above link. – Jarvis Jul 20 '18 at 12:39