1

I tried to learn about Firebase. And I successfully received notification from it. But how I can open another activity when click on the notification?

This my code:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
    NotificationCompat.Builder builder = new  NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("test")
            .setContentText(remoteMessage.getData().get("message"));
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());



}
AL.
  • 36,815
  • 10
  • 142
  • 281
  • Are you sending notification from Firebase console? You need to use Data messages – Tosin Onikute Oct 04 '16 at 14:59
  • Possible duplicate of [Firebase FCM notifications click\_action payload](http://stackoverflow.com/questions/37407366/firebase-fcm-notifications-click-action-payload) – AL. Oct 05 '16 at 05:50
  • as you like, i find the solution here –  Oct 05 '16 at 09:38

2 Answers2

0

It's sth about notification, nothing to do firebase.

check this you need to Define the Notification's Action

Chenmin
  • 121
  • 7
0

You need to build a PendingIntent and notify using NotificationManager

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

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

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

As Tosin suggested, if you're sending push notification from Firebase console, you need to put your payload into data key

lubilis
  • 3,942
  • 4
  • 31
  • 54