简体   繁体   中英

How to start a fragment instead of an activity (Intent) from showNotification() function?

I´m sending messages from a server to mobile devices using Firebase Cloud Messaging , so I´ve created a class that extends from FirebaseMessagingService and overrides onMessageReceived(RemoteMessage remoteMessage) from which I´m calling an implemented method notifyUser(String from, String notification) . This method, in turn, calls another method showNotification(String from, String notification, Intent intent) that belongs to class myNotificationManager in which I´m creating the notification. The problem is that I need to start a fragment instead of an activity. Any ideas of how to do it? Thanks in advance.

public class ServicioFirebaseMessaging extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage){   
              notifyUser(remoteMessage.getFrom(),remoteMessage.getNotification().getBody());}

public void notifyUser(String from, String notification){

//Create an object of the class that contain the method which creates the notification

 MyNotificationManager myNotificationManager = new MyNotificationManager(getApplicationContext());

//Create an Intent that will be passed as a parameter in the method that creates the
//notification. This intent will be executed when user clicks on the notification. ShowMessage is a class in which 
//I define some TextView and other elements to show the message

Intent intent = new Intent(getApplicationContext(),ShowMessage.class); 

//put some values in the intent
//.......................

//calls the method that creates the notification
  myNotificationManager.showNotification(from,notification,intent);
  }
}

The code from class MyNotificationManager :

public class MyNotificationManager {

//Activity context
private Context ctx;
//Notification id.
public static final int NOTIFICATION_ID = 234;
//Sound for notification
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

//Constructor
public MyNotificationManager(Context ctx) {
    this.ctx = ctx;
}

public void showNotification (String from, String notification, Intent intent){
//Define an Intent and actions to perform when called
    PendingIntent pendingIntent = PendingIntent.getActivity(
            ctx,
            NOTIFICATION_ID,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    //To configre the notification
    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
    Notification mNotification = builder.setSmallIcon(R.mipmap.ic_launcher)
            //define notification parameters
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setContentTitle(from)
            .setContentText(notification)
            .setSound(uri)
            .setSmallIcon(R.mipmap.ic_face)
            .setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(),R.mipmap.ic_face))
            .build();

    //Define notification flags
    mNotification.flags |= Notification.FLAG_AUTO_CANCEL;

    //Notify user about received message
    NotificationManager notificationManager = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);

    //Execute notification
    notificationManager.notify(NOTIFICATION_ID,mNotification);
 }

}//end of class

I´ve found a way to do it. Instead of make an Intent to call ShowMessage.class, I´m calling MainActivity.class. Now MainActivity (App in general) could be started in two ways:

  1. Default access to App: User taps on app´s icon and access the app (MainActivity).

  2. Users tap on notification and access the app (MainActivity) through an Intent implemented on notifyUser(String from, String notification) method, in ServicioFirebaseMessaging class which extends FirebaseMessagingService. In this Intent I´m passing some values that will be taken on MainActivity.

The difference now is that I´ve implemented extra operations in MainActivity, Sorrounded by a try/catch with and 'if' condition inside, in a way that if user access app in the first way, no Intent was performed then no data to take from the Intent cause it doesn´t even exist (getIntent().getExtras.getSomething() will fail), Because this operations are sorrounded by a try/cath, the app doesn´t fall. If user access app in the second way, operations inside the try/catch will take place because 'if' condition will be true, and in this set of operations is where the calling to the fragment in question(FragmentSelectedMessage) are performed, passing to the fragment the values of the Intent. Now, of course, the fragment has an activity that is supporting it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM