5

When user taps on a notification sent from the Firebase console, I need to launch a specific Android Activity, only when the user taps on the notification. How can this be accomplished in the following 2 scenarios:

  1. App is not opened or app running in Background
  2. App is in the foreground
Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
  • 1
    There is currently no way to control what activity gets started when the notification is tapped. See http://stackoverflow.com/questions/37554274/open-app-on-firebase-notification-received-fcm – Frank van Puffelen Jun 09 '16 at 16:49
  • Hi Frank, can you also please tell me how to send a message/notification in the form of json from firebase. – Anuroop Kakkirala Jun 10 '16 at 03:24
  • 1
    You'll need to send a downstream message from a server. See https://firebase.google.com/docs/cloud-messaging/downstream – Frank van Puffelen Jun 10 '16 at 04:03
  • @FrankvanPuffelen Thank you for the prompt response. How to send the downstream message with data(json), either from Firebase console or from any 3rd party cloud push platform, as I don't have access to any in-house server for the same? – Anuroop Kakkirala Jun 10 '16 at 10:33
  • In the advanced section of the FIrebase composer you will be able to add key value pairs that get passed to the launched activity as intent extras. – Arthur Thompson Jun 17 '16 at 10:52

1 Answers1

4

You can achieve this in two ways,

1. First way

Adding click_action in notification payload, jNotification.put("click_action", "OPEN_ACTIVITY_1");

private void pushNotification(String token) {
        JSONObject jPayload = new JSONObject();
        JSONObject jNotification = new JSONObject();
        JSONObject jData = new JSONObject();
        try {
            jNotification.put("title", "Google I/O 2016");
            jNotification.put("text", "Firebase Cloud Messaging (App)");
            jNotification.put("sound", "default");
            jNotification.put("badge", "1");
            jNotification.put("click_action", "OPEN_ACTIVITY_1");

            jData.put("picture_url", "http://opsbug.com/static/google-io.jpg");

            jPayload.put("to", token);
            //jPayload.put("to", "/topics/news");
            //jPayload.put("condition", "'logined' in topics || 'news' in topics");
            //jPayload.put("registration_ids", jData);
            jPayload.put("priority", "high");
            jPayload.put("notification", jNotification);
            jPayload.put("data", jData);

            URL url = new URL("https://fcm.googleapis.com/fcm/send");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Authorization", AUTH_KEY);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setDoOutput(true);

            // Send FCM message content.
            OutputStream outputStream = conn.getOutputStream();
            outputStream.write(jPayload.toString().getBytes());

            // Read FCM response.
            InputStream inputStream = conn.getInputStream();
            final String resp = convertStreamToString(inputStream);

            Handler h = new Handler(Looper.getMainLooper());
            h.post(new Runnable() {
                @Override
                public void run() {
                    Log.e("Response", resp);
                    //txtStatus.setText(resp);
                }
            });
        } catch (JSONException | IOException e) {
            e.printStackTrace();
        }
    }

Add this is in your AndroidManifest.xml

        <activity android:name="com.example.fcm.SecondActivity">
            <intent-filter>
                <action android:name="OPEN_ACTIVITY_1" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

MyFirebaseMessagingService.java

  public class MyFirebaseMessagingService extends FirebaseMessagingService {
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);

            // 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. See sendNotification method below.

            RemoteMessage.Notification notification = remoteMessage.getNotification();
            Map<String, String> map = remoteMessage.getData();

            sendNotification(notification.getTitle(), notification.getBody(), map);
        }

        private void sendNotification(String title, String body, Map<String, String> map) {
            Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

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

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setContentIntent(pendingIntent)
                    .setContentInfo(title)
                    .setLargeIcon(icon)
                    .setSmallIcon(R.mipmap.ic_launcher);

            try {
                String picture_url = map.get("picture_url");
                if (picture_url != null && !"".equals(picture_url)) {
                    URL url = new URL(picture_url);
                    Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                    notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(body));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
            notificationBuilder.setLights(Color.YELLOW, 1000, 300);

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notificationBuilder.build());
        }
    }

SecondActivity.java

public class SecondActivity extends AppCompatActivity {
    private static final String TAG = "SecondActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            for (String key : bundle.keySet()) {
                Object value = bundle.get(key);
                Log.d(TAG, "Key: " + key + " Value: " + value.toString());
            }
        }
    }
}

2. Second way

Send key through data payload like below and get key in MainActivity via getIntent() and call specific activity or fragments.

        json1.put("title","Your Title");  
        json1.put("body","body content");  
        json1.put("message","Your Message");  
        json1.put("screen","2");   //secondFragment is 2nd position in nav drawer
        json.put("data", json1); 

MainActivity.java

Intent intent = getIntent();
        String pos = getIntent().getStringExtra("screen");
        if(pos !=null){
            selectDrawerItem(navigationView.getMenu().getItem(Integer.parseInt(pos)));
        }

Sample project in GITHUB, https://github.com/Google-IO-extended-bangkok/FCM-Android

Chandrahasan
  • 1,583
  • 19
  • 24